Lambda Expression is one of the most important features come with the release of Java 8 version. It gives the way java can also deal with the functional approach. The aim behind this invention is to keep java in a row with all others functional oriented languages , like JavaScript . Lambda Expression is a concept of an anonymous function ( a function without name ), which wraps with the functional interface with its object creation.
Syntax of lambda Expression :
( arg1, arg2,......argN ) - > { // No of statements };
It contains :
1. Zero or more number of parameters separated by comma within parenthesis.
2. -> is a token indicating lambda expression.
3. After -> contains one or more number of statements within curly braces.
Functional Interface :
It is an interface containing single abstract method only. It cannot contain more than one abstract method, but can have methods of Object class. Finally a functional interface can contain only one method or if it is containing more then one method then all others methods should be from Object class only.
For ex- Comparator interface of java.util package.
public interface Comparator {
int compare(Object, Object); //Own method
boolean equals(Object); // From Object class
}
Comparison with Anonymous class
Anonymous class is also an expression. Anonymous class implementing single interface functions equally as compared to lambda expression, but considering writing code it looks unclear and little confusing.
* Passing functionality as a method argument is not possible with the Anonymous class which is with the Lambda Expression.
* With the Lambda expression we can able to pass functionality with the methods as a argument after the creation of instance for the functional interface.
* No need to writing new operator for creating the instance of its functional interface, it is created internally by the JVM. But we need to do in case of writing anonymous class expression.
* No need to override the abstract method of the functional interface because lambda expression is itself an method implementing the code for that abstract method. But need to override in the anonymous class curly braces.
Task performed by Lambda Expression
After declaring its expression, it basically perform two important tasks :
1. Creating the instance of functional interface to which it wraps and return the instance to its reference.
2. After executing the blocks within the parenthesis it returns the required value or void if not returning anything.
Note : Lambda Expression is an expression so it need to be assigned to the right side declaration statement which should be the reference of the functional interface because lambda expression first returns the instance of it.
Example showing Lambda Expression
public class Hello {
interface hi{
String display(); //abstract method with
String return type
}
public static void main(String args[]){
hi h= () ->{ //Start of Expression
return "Lambda Expression"; //returns String
}; //End of Expression
System.out.println(h.display()); //Display the returned value
}
}
Step of execution of the above example :
1. hi is the Functional interface.
2. Lambda expression wraps with the functional interface.
3. Creates the instance of interface " hi " .
4. Calls the display method.
5. After calling executes the Lambda expression and returns the String to the calling method " display ".
6. Print the returned value.
Note:
* Arguments of the lambda expression depends on the abstract method present in the functional interface,otherwise give compilation error saying,
lambda expression's signature doesn't match the signature of functional interface method.
* If Lambda Expression is having single statement then no need to writing curly braces.For ex :
() -> System.out.println("lambda expression");//Print string
() -> "lambda expession" ; // returns String
() -> 10; // returns int value
* Lambda Expression must return either required value or void.
* Lambda Expression doesn't have their own scope because it is just a method implementation.
0 comments:
Post a Comment