Section 17.2.2 Lambda Expressions

• A lambda expression (p. 733) represents an anonymous method—a shorthand notation for implementing a functional interface.

• The type of a lambda is the type of the functional interface that the lambda implements.

• Lambda expressions can be used anywhere functional interfaces are expected.

• A lambda consists of a parameter list followed by the arrow token (->, p. 733) and a body, as in:

(parameterList) -> {statements}

For example, the following lambda receives two ints and returns their sum:

(int x, int y) -> {return x + y;}

This lambda’s body is a statement block that may contain one or more statements enclosed in curly braces.

• A lambda’s parameter ...

Get Java™ How To Program (Early Objects), Tenth 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.