What is Lambda Expressions?
Ans:- Java 8 has introduced a new feature called Lambda expressions. It is considered to be a major change in Java because it introduces a functional programming into Java.
Other languages such as Python, C# and C++ already have this feature so this is not a new to programming world.
Although, it is new to Java as it is the last language to support this feature.
Lisp is the second programming language in the world which was to support this feature first time.
Definition:- A Lambda Expression can be understood as a concise representation of an anonymous function that can be passed around.
To properly understand this definition, we must break it down :-
Anonymous— We say anonymous because it doesn’t have an explicit name like a method would normally have; less to write and think about.
Function— We say function because a lambda isn’t associated with a particular class. But like a method or function, a lambda has a list of parameters, a body and a return value.
Passed around— A lambda expression can be passed as argument to a method or stored in a variable.
Concise— We don’t need to write a lot of boilerplate code like we do when we implement an interface.
To understand Lambdas , let’s recall what are the components of a method in Java-
Normally a method in Java has following parts:-
Access Modifier
Return Type
Method Name
Parameter list
List Of Exceptions It Can Throw
Body
A Lambda Expression in Java just has 3 main parts:-
Parameter List
Body
Return Value (if needed)
Why we use Lambda Expression?
1) To provide the implementation of Functional interface.
2) Less coding.
3) It is very useful in Collection framework.
4) It saves a lot of time.
The lambda body can be of two types:-
Ans:- Java 8 has introduced a new feature called Lambda expressions. It is considered to be a major change in Java because it introduces a functional programming into Java.
Other languages such as Python, C# and C++ already have this feature so this is not a new to programming world.
Although, it is new to Java as it is the last language to support this feature.
Lisp is the second programming language in the world which was to support this feature first time.
Definition:- A Lambda Expression can be understood as a concise representation of an anonymous function that can be passed around.
To properly understand this definition, we must break it down :-
Anonymous— We say anonymous because it doesn’t have an explicit name like a method would normally have; less to write and think about.
Function— We say function because a lambda isn’t associated with a particular class. But like a method or function, a lambda has a list of parameters, a body and a return value.
Passed around— A lambda expression can be passed as argument to a method or stored in a variable.
Concise— We don’t need to write a lot of boilerplate code like we do when we implement an interface.
To understand Lambdas , let’s recall what are the components of a method in Java-
Normally a method in Java has following parts:-
Access Modifier
Return Type
Method Name
Parameter list
List Of Exceptions It Can Throw
Body
Return Value
A Lambda Expression in Java just has 3 main parts:-
Parameter List
Body
Return Value (if needed)
Why we use Lambda Expression?
1) To provide the implementation of Functional interface.
2) Less coding.
3) It is very useful in Collection framework.
4) It saves a lot of time.
The lambda body can be of two types:-
1. A
body with a single
expression
2. A
body with a block
of code
Initially we will discuss single expression lambda’s and then move on lambda’s with block of code.
Converting Normal Method To Lambda :-
Parameters with Modifiers:-
We can use modifiers, such as final, in the parameter declaration for lambda expressions.
The following two lambda expressions are valid:-
The following lambda expression will not compile because it uses the final modifier in parameter declarations, but omits the parameter type:-
( final x , final y) ---> { return x+y; }
How Lambda Expressions are Used?
A Lambda Expression is never executed on it’s own. It is always used with functional interfaces.
As mentioned earlier, a lambda expression is not executed on it’s own.
Initially we will discuss single expression lambda’s and then move on lambda’s with block of code.
Converting Normal Method To Lambda :-
Parameters with Modifiers:-
We can use modifiers, such as final, in the parameter declaration for lambda expressions.
The following two lambda expressions are valid:-
The following lambda expression will not compile because it uses the final modifier in parameter declarations, but omits the parameter type:-
( final x , final y) ---> { return x+y; }
How Lambda Expressions are Used?
A Lambda Expression is never executed on it’s own. It is always used with functional interfaces.
So, before we understand how to use a lambda expression, we must first understand what is a functional interface.
To learn functional interface:-
click the below link:-
Using Lambda Expressions With Functional Interface:-
As mentioned earlier, a lambda expression is not executed on it’s own.
Rather, it is always
used with functional interface to provide the implementation of the abstract method defined by the functional interface.
The benefit of using Lambda Expressions with functional interface is that they make implementation of the abstract method short , readable and concise.
To understand this
benefit , let’s create a functional
interface and implement it in older way.
Example:-
The Output-
Greetings: Good Morning!
Explanation:-
When the compiler
comes across the following statement:-
Greetings
gr=(){ return “Good
Morning!”; };
It takes the
following actions:-
1) It reads the name of
the functional interface Greetings and realizes that
the lambda expression on the right side of
assignment operator is actually overriding the method greet().
2) This is because the functional interface Greetings contains only one
abstract method i.e.
greet().
3) Now the compiler automatically creates
a class that implements the functional interface Greetings, overrides the
abstract method greet() inside the class and
puts the definition given by the lambda expression in the body of that method.
4) Then the compiler automatically creates
an object of that anonymous class and assigns it to
the Greetings
reference gr.
5) Finally when we call
the method by writing the line gr.greet(), the lambda expression is executed.
Thus, we can say that, a lambda expression gives us a way to transform
a code segment into an object.
Block level Lambda's:-
The body of the lambdas shown in the preceding examples consist of a single expression.
These types of lambda bodies are referred to as expression bodies, and lambdas that have expression bodies are sometimes called expression lambdas.
In an expression body, the code on the right side of the lambda operator must consist of a single expression, which becomes the lambda’s value.
Although expression lambdas are quite useful, sometimes the situation will require more than a single expression.
To handle such cases, Java supports a second type of lambda expression in which the code on the right side of the lambda operator consists of a block of code that can contain more than one statement.
This type of lambda body is called a block body. Lambdas that have block bodies are sometimes referred to as block level lambda's.
Syntax:-
When we create block lambdas these must be enclosed in curly brackets ( a code block) and the return type of the lambda is same as the type of the value returned within the code block, or void if nothing is returned.
Block level Lambda's:-
The body of the lambdas shown in the preceding examples consist of a single expression.
These types of lambda bodies are referred to as expression bodies, and lambdas that have expression bodies are sometimes called expression lambdas.
In an expression body, the code on the right side of the lambda operator must consist of a single expression, which becomes the lambda’s value.
Although expression lambdas are quite useful, sometimes the situation will require more than a single expression.
To handle such cases, Java supports a second type of lambda expression in which the code on the right side of the lambda operator consists of a block of code that can contain more than one statement.
This type of lambda body is called a block body. Lambdas that have block bodies are sometimes referred to as block level lambda's.
Syntax:-
When we create block lambdas these must be enclosed in curly brackets ( a code block) and the return type of the lambda is same as the type of the value returned within the code block, or void if nothing is returned.
1 Comments
Lambda expression internally create anonymous class then create corresponding a .class file but in case of lambda expression not create any .class file
ReplyDelete