Lambda Expression In Java 8 Features
This feature is introduced in Java 8. This feature is concise lines of code and enables functional programming.
Lambda Expression:
• It is an anonymous function
• Without return type
• Without modifiers
• With special symbol “->” arrow
Example:
public void m(){
System.out.println("Hello");
}
() -> System.out.println("Hello");
→ If the lambda expression method body contains one line then curly “{}” braces are optional and contains multiple lines then curly braces are mandatory.
() -> System.out.println("Hello");
Example:
public void m(int a,int b){
System.out.println(a*b);
}
(int a,int b) -> System.out.println(a*b);
→ Sometimes the compiler can guess the type of parameter automatically. In that case, we don’t need to specify the type of parameter explicitly.
(a,b) -> System.out.println(a*b);
Example:
public int square(int n) {
return n * n;
}
(n) -> {
return n * n;
}
→ Without curly “{}” braces we don’t need to specify “return” explicitly. But with curly “{}” braces “return” is mandatory.
(n) -> n*n;
→ If the input parameter is single then the parenthesis “()” is optional.
n -> n*n;
Note: Lambda Expression is used to provide an implementation of an interface which is a Functional Interface. Lambda Expression will not generate a “.class ” file due to this performance increase.
Example:
interface Square {
void square(int x);
}
//Implementation class of interface
class SquareImpl implements Square {
@Override
public void square(int x) {
System.out.println("Square Of No. is:" + x * x);
}
}
public class Test {
public static void main(String[] args) {
Square sq = new SquareImpl();
sq.square(10);
}
}
Output:
Square Of No. is:100
→ In the above example without Lambda Expression, we can implement an Interface through the extra class implementation of an Interface.
→ In the above example, the “Square” interface has a single abstract method. So, the “Square” interface is a Functional Interface.
→ In this example it also generates an extra “.class” file i.e., SquareImpl.class.
Example:
interface Square {
void square(int x);
}
public class Test {
public static void main(String[] args) {
//Anonymous Inner class
Square sq = new Square() {
@Override
public void square(int x) {
System.out.println("Square Of No. is:" + x * x);
}
};
sq.square(10);
}
}
Output:
Square Of No. is:100
→ In the above example, we can implement an interface with an anonymous inner class.
→ In the above example it also generates an extra “.class” file.
→ In the above example length of code is decreased as compared to the extra implementation of an interface.
Example:
interface Square {
void square(int x);
}
public class Test {
public static void main(String[] args) {
//Lambda Expression
Square sq = (x) -> System.out.println("Square Of No. is:" + x * x);
sq.square(10);
}
}
Output:
Square Of No. is:100
→ In the above example, we can implement that interface through Lambda Expression because the “Square” interface is a Functional Interface. Here we can concise the code through this Java 8 feature.
→ Lambda Expression is enabling functional programming in the code.
→ Lambda Expression helps without generating an extra class to implement the functional interface.
→ Lambda Expression helps without generating an anonymous inner class to implement the functional interface.
→ In the above example not generating an extra “.class” file.
→ In the above example length of code decreases as compared to the extra implementation class and an anonymous inner class of functional interface.
Conclusion:
This topic is explained What is Lambda Expression? How can we write Lambda Expression? How to identify Lambda Expression? How to use Lambda Expression?
Leave your thought here
Your email address will not be published. Required fields are marked *