Engineering Full Stack Apps with Java and JavaScript
Java 8 allows you to write lambda expressions in few varying syntaxes. Let us quickly see those here.
Runnable r = () -> System.out.println(“H”);
ActionListener al1 = (event) -> System.out.println(“Button clicked”);
ActionListener al2 = event -> System.out.println(“Button clicked”);
Runnable r = () -> {
System.out.print(“Hello ”);
System.out.print(“World”);
};
BinaryOperator<Long> add = (x,y) -> x+y;
BinaryOperator<Long> add = (Long x, Long y) -> x+y;
Return type is not needed for one line statement lambda expressions.
For a block of code with paranthesis, it will be required.
A lambda expression that uses a variable from the enclosing scope (e.g. method argument).
However, these variables should be effectively final.
Give examples where explicit typing may be required.
Lambda expression for a functional interface that throws exception.
Give example for method paameterization: method that has an argument of type a functional interface, can receive a lambda expression.
Give example for implicit and explicit return.
Lambda expression that uses this, super etc. for accessing variables. Should these be effectively final?