
Lambda Expression is way to passing a block of code to a method. To understand this concept we need to implement the functional interface and anonymous class which was used in the earlier version of the Java.
“Lambda Expression term is come from Mathematics, Greek letter Lambda basically associated with some kind of analogous situation where we are passing one function as a parameter to another function.”
interface Executable {
void execute();
}
class Runner {
public void run(Executable e) {
System.out.println("Executing Code Block…");
e.execute();
}
}
public class App {
public static void main(String args[]) {
Runner runner = new Runner();
runner.run(new Executable() {
public void execute() {
System.out.println("Anonymous Class Code Block.");
}
});
}
}
The output of above code is
Anonymous Class Code Block.
same thing we can implement with the help of Lambda Expression in following manner,
we need to replace above mentioned anonymous class code block
public void execute() {
System.out.println("Anonymous Class Code Block.");
}
});
with following Lambda Expression,
we get same output from Lambda Expression, i.e.
Lambda Expression Code Block.
Hence by using Lambda Expression in Java, we are reducing code complexity and increasing the readability of our application.
Hope, you will get basic idea of Lambda Expression by above mentioned example. Below are some important points which will help you to clear understanding of Lambda Expressions in Java 8.
A) We can replace the Anonymous class with our Lambda Expression.
The below mentioned method
is replaced the whole anonymous class which is mentioned in above example. i.e.
public void execute(){
System.out.println("Anonymous Class Code Block.");
}
});
where,
method name which is declared in Runner class.
List of Parameters required for Lambda Expression.
Code block which to be executed.
B) If we want to execute a multiple statements with the Lambda Expression then include the { and } parenthesis to code which you need to be executed. If you are adding multiple statements then it is mandatory to terminate the each statement with the “;”. If you are using { and } parenthesis and you have added single statement only then that single statement is also needs to be terminated with the help of “;”.
exa. Comparable and Runnable interface.
C) If we want return value from block of code that is going to be execute,
interface Executable{
int execute();
}
class Runner{
public void run(Executable e){
System.out.println("Executing Code Block…");
int value = e.execute();
System.out.println("Return value is : "+value);
}
}
public class App{
public static void main(String args[]){
Runner runner = new Runner();
runner.run(new Executable (){
public int execute(){
System.out.println("Anonymous Class Code Block.");
return 7;
}
});
}
}
output of above example is,
Anonymous Class Code Block.
Return value is : 7
same thing we can implement with the help of Lambda Expression in following manner,
Hence, if we replace the above code in our example then we get following output after execution of program,
Lambda Expression Code Block.
Return value is : 8
D) If we are returning only literal value from Lambda Expression then no need to mentioned the datatype of return value.
E) If we want to pass parameter,
interface Executable {
int execute(int a);
}
class Runner {
public void run(Executable e) {
System.out.println("Executing Code Block…");
int value = e.execute(12);
System.out.println("Return value is : " + value);
}
}
public class App {
public static void main(String args[]) {
Runner runner = new Runner();
runner.run(new Executable() {
public int execute(int a) {
System.out.println("Anonymous Class Code Block.");
return 7 + a;
}
});
}
}
For above mentioned program, we get following output,
Anonymous Class Code Block.
Return value is : 19
same thing we can implement with the help of Lambda Expression in following manner,
we will get following output if we replace anonymous class with our Lambda Expression,
Lambda Expression Code Block.
Return value is : 19
F) If we are using Lambda Expression then there is no need to define parameter type. Our Compiler will autodetect the type of parameter from the declaration of method from calling class.
G) In case of method overloading, we need to specify the datatype of parameters to avoid the ambiguity between overloading methods.
interface Executable{
int execute(int a);
}
interface StringExecutable{
int execute(String a);
}
class Runner{
public void run(Executable e){
System.out.println("Executing Code Block…");
int value = e.execute(12);
System.out.println("Return value is : "+value);
}
public void run(StringExecutable e){
System.out.println("Executing Code Block…");
int value = e.execute("Hello");
System.out.println("Return value is : "+value);
}
}
public class App{
public static void main(String args[]){
Runner runner = new Runner();
runner.run(new Executable (){
public int execute(int a){
System.out.println("Anonymous Class Code Block.");
return 7 + a;
}
});
}
}
so after execution of this code, we will get following output,
Anonymous Class Code Block.
Return value is : 19
same thing we can implement with the help of Lambda Expression in following manner,
so after replacing and executing of our Lambda Expression, we will get following output,
Lambda Expression Code Block.
Return value is : 19
H) If we have single parameter then no need to define the datatype of parameter as well as no need to add parenthesis for parameters.
I) If we want to pass multiple parameters then we need to add parenthesis compulsory.
J) If we want to access the variable from main (parent) method in the block of Lambda Expression.
same thing we can implement with the help of Lambda Expression in following manner,
K) Lambda Expression doesn’t have their own scope, so we can access variable from calling method, while in previous version of Java, anonymous class having their own scopes, so you can re-declare the variable with same declaration as declared in the parent method.
This variable in the anonymous class is treated as new variable.
same thing we can implement with the help of Lambda Expression in following manner,
System.out.println("Lambda Expression Code Block.");
//int d=5; we can’t do this as this is no new scope.
return a+b+c;
});
L) We can store all the Lambda Expression into equivalent object also.
System.out.println("Lambda Expression Code Block.");
return a+b;
}
runner.run(ex);
M) We can assign the Lambda Expression to superclass of interface, for that we need to typecast that object to superclass object type.
This is very useful to implement comparable interface.
So this is some basic and simple Lambda Expressions which is used in Java 8. We will see some real time examples of Lambda Expressions in our Next Post.
Hope this blog will help you to understand the basic concept of Lambda Expressions in Java 8.
Please donβt forget to comment your feedback for this post. π
References : Video of Cave of Programming.
That’s great. keep it up dude!
Thanks Suraj!
Nice blog and very easy to understand
Thanks Suhas.
can i refer to a final variable using lambda expression and if so what it will throw if another value is assigned to that variable i.e a compilation error or any pre-defined exception.
Hi Akshay,
As I have already explained in the blog, Lambda Expressions doesn’t have any new scope with respect to variables so by default each variable is treated as Final Variable. So assigning new value to final variable throws compile time exception.
nic 1 Mayur helped me alot π
Thank you.