The term “statement expression” or “expression statement” refers to expressions that are also allowed to be used as a statement. They are described in the Java Language Specification, §14.8. Expression Statements.
They include:
- Method Invocations
- Assignments
- Increment/Decrement expressions
- Class Instance Creation expressions
So other examples are:
Consumer<String> b = s -> counter++;
Function<String,Integer> f = s -> counter++;
or
Consumer<String> b = s -> new BigDecimal(s);
Function<String,BigDecimal> f = s -> new BigDecimal(s);
As a rule of thumb, a lambda expression of the form x -> expression
is only legal for a Consumer
(or void
function type in general), if x -> { expression; }
would be legal too.