---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 21 Edition"
java_version: 21
chapter: "Chapter 15. Expressions"
section: "15.27.3"
heading: "15.27.3. Type of a Lambda Expression"
parent: "15.27. Lambda Expressions"
source: https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.27.3
id: "15.27.3"
url: https://jvm.obfuscated.software/jls/se21/15.27.3.html
raw: https://jvm.obfuscated.software/jls/se21/15.27.3.md
---

# 15.27.3. Type of a Lambda Expression

A lambda expression is compatible in an assignment context, invocation context, or casting context with a target type T if T is a functional interface type ([§9.8](9.8.md)) and the expression is *congruent* with the function type of the *ground target type* derived from T.

The *ground target type* is derived from T as follows:

- If T is a wildcard-parameterized functional interface type and the lambda expression is explicitly typed, then the ground target type is inferred as described in [§18.5.3](18.5.3.md).
- If T is a wildcard-parameterized functional interface type and the lambda expression is implicitly typed, then the ground target type is the non-wildcard parameterization ([§9.9](9.9.md)) of T.
- Otherwise, the ground target type is T.

A lambda expression is *congruent* with a function type if all of the following are true:

- The function type has no type parameters.

- The number of lambda parameters is the same as the number of parameter types of the function type.

- If the lambda expression is explicitly typed, its formal parameter types are the same as the parameter types of the function type.

- If the lambda parameters are assumed to have the same types as the function type's parameter types, then:

  - If the function type's result is `void`, the lambda body is either a statement expression ([§14.8](14.8.md)) or a `void`-compatible block.
  - If the function type's result is a (non-`void`) type R, then either (i) the lambda body is an expression that is compatible with R in an assignment context, or (ii) the lambda body is a value-compatible block, and each result expression ([§15.27.2](15.27.2.md)) is compatible with R in an assignment context.

If a lambda expression is compatible with a target type T, then the type of the expression, U, is the ground target type derived from T.

It is a compile-time error if any class or interface mentioned by either U or the function type of U is not accessible ([§6.6](6.6.md)) from the class or interface in which the lambda expression appears.

For each non-`static` member method `m` of U, if the function type of U has a subsignature of the signature of `m`, then a notional method whose method type is the function type of U is deemed to override `m`, and any compile-time error or unchecked warning specified in [§8.4.8.3](8.4.8.3.md) may occur.

A checked exception that can be thrown in the body of the lambda expression may cause a compile-time error, as specified in [§11.2.3](11.2.3.md).

The parameter types of explicitly typed lambdas are required to exactly match those of the function type. While it would be possible to be more flexible - allow boxing or contravariance, for example - this kind of generality seems unnecessary, and is inconsistent with the way overriding works in class declarations. A programmer ought to know exactly what function type is being targeted when writing a lambda expression, so the programmer should thus know exactly what signature must be overridden. (This is not the case for method references, so more flexibility is allowed when they are used.) In addition, more flexibility with parameter types would add to the complexity of type inference and overload resolution.

Note that while boxing is not allowed in a strict invocation context, boxing of lambda result expressions is *always* allowed - that is, the result expression appears in an assignment context, regardless of the context enclosing the lambda expression. However, if an explicitly typed lambda expression is an argument to an overloaded method, a method signature that avoids boxing or unboxing the lambda result is preferred by the most specific check ([§15.12.2.5](15.12.2.5.md)).

If the body of a lambda is a statement expression (that is, an expression that would be allowed to stand alone as a statement), it is compatible with a `void`-producing function type; any result is simply discarded. So, for example, both of the following are legal:

```
// Predicate has a boolean result
java.util.function.Predicate<String> p = s -> list.add(s);
// Consumer has a void result
java.util.function.Consumer<String> c = s -> list.add(s);
```

Generally speaking, a lambda of the form `()` `->` *expr*, where *expr* is a statement expression, is interpreted as either `()` `->` `{` `return` *expr*`;` `}` or `()` `->` `{` *expr*`;` `}`, depending on the target type.
