Lambda Expression
Lambda Expression
E.g.:
System.out.println(“Hello World”);
Benefits
E.g.:
return (a+b);
return (a+b);
};
Rules
1. If the body of lambda contains only one statement, then curly braces are optional
2. Java compiler infer the type of variables passed in arguments, hence type is optional
return str.length();
Functional Interface
If the interface contains only one abstract method then it is a functional interface.
e.g.
Approach-1
Functional interface
@Override
System.out.println(“Hello”);
myInter.sayHello();
@Override
};
myInter1.sayHello(); //Output is “First Anonymous class”
//so in case functional interface is having several differing implementations then using approach-1
//several classes have to defined to implement several differing implementations but using
//approach-2 i.e. anonymous class the method follows
@Override
};
};
};
In above 03 approaches approach-3 is simplest, min. lines of code and clear way of implementing the
functional interface.