Default Methods


Idioms and Techniques

Design Rationale

Advanced Questions

What is the type of a lambda expression?

A lambda expression is an instance of a functional interface. But a lambda expression itself does not contain the information about which functional interface it is implementing; that information is deduced from the context in which it is used. For example, the expression

    x -> 2 * x 

can be an instance of the functional interface

    interface IntOperation { int operate(int i); } 

so it is legal to write

    IntOperation iop = x -> x * 2; 

The type expected for the expression on the right-hand side of the assignment here is IntOperation. This is called the target type for the lambda expression. Clearly a lambda expression can be type-compatible with different functional interfaces, so it follows that the same lambda expression can have different target types in different contexts. For example, given an interface

    interface DoubleOperation { double operate(double i); } 

it would also be legal to write

    DoubleOperation dop = x -> x * 2; 

The target type for a lambda expression must be a functional interface and, to be compatible with the target type, the lambda expression must have the same parameter types as the interface’s function type, its return type must be compatible with the function type, and it can throw only exceptions allowed by the function type.
An interface having only a single
abstract method: see this page

Roughly, the type of its single abstract method; for a more
precise explanation see the syntax notes to this page