Lambda Body
A lambda body is either a single expression or a statement block. Execution of a lambda body either has a non-void return (i.e., its evaluation returns a value), or has a void return (i.e., its evaluation does not return a value), or its evaluation throws an exception.
A single-expression lambda body is used for short and succinct lambda expressions. A single-expression lambda body with a void return type is commonly used to achieve side effects. The return keyword is not allowed in a single-expression lambda body.
In the examples below, the body of the lambda expressions is an expression whose execution returns a value—that is, has a non-void return.
() -> 2021 // Expression body, non-void return
() -> null // Expression body, non-void return
(i, j) -> i + j // Expression body, non-void return
(i, j) -> i <= j ? i : j // Expression body, non-void return
str -> str.length() > 3 // Expression body, non-void return
str -> str != null // Expression body, non-void return
&& !str.equals(“”) && str.length() > 3
&& str.equals(new StringBuilder(str).reverse().toString())
In the following examples, the lambda body is an expression statement that can have a void or a non-void return. However, if the abstract method of the functional interface does not return a value, the non-void return of an expression statement body can be interpreted as a void return—that is, the return value is ignored.
val -> System.out.println(val) // Method invocation statement, void return
sb -> sb.trimToSize() // Method invocation statement, void return
sb -> sb.append(“!”) // Method invocation statement, non-void return
() -> new StringBuilder(“?”) // Object creation statement, non-void return
value -> value++ // Increment statement, non-void return
value -> value *= 2 // Assignment statement, non-void return
The following examples are not legal lambda expressions:
(int i) -> while (i < 10) ++i // Illegal: not an expression but statement
(x, y) -> return x + y // Illegal: return not allowed in expression
The statement block comprises declarations and statements enclosed in curly brackets ({}). The return statement is only allowed in a block lambda body, and the rules are the same as those in a method body: A return statement with an argument can only be used for a non-void return and its use is mandatory, whereas a return statement with no argument can only be used for a void return, but its use is optional. The return statement terminates the execution of the lambda body immediately.
Click here to view code image () -> {} // Block body, void return
() -> { return 2021; } // Block body, non-void return
() -> { return 2021 } // Illegal: statement terminator (;) in block missing
() -> { new StringBuilder(“Go nuts.”); } // Block body, void return
() -> { return new StringBuilder(“Go nuts!”); } // Block body, non-void return
(int i) -> { while (i < 10) ++i; } // Block body, void return
(i, j) -> { if (i <= j) return i; else return j; } // Block body, non-void return
(done) -> { // Multiple statements in block body, void return
if (done) {
System.out.println(“You deserve a break!”);
return;
}
System.out.println(“Stay right here!”);
}