... multiplies its value by 2 and returns the result. In this case, the body is a statement block that may contain statements enclosed in curly braces. The compiler infers from the lambda that it returns an int, because the parameter x is an int and the literal 2 is an int—multiplying an int by an int yields an int result. As in a method declaration, lambdas specify parameters in a comma-separated list. The preceding lambda is similar to the method

int multiplyBy2(int x) {
   return x * 2;
}

but the lambda does not have a name and the compiler infers its return type. There are several variations of the lambda syntax.

Eliminating a Lambda’s Parameter Type(s)

A lambda’s parameter type(s) usually may be omitted, as in:

(x) -> {return x * 2;}

in which ...

Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.