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

# 15.27. Lambda Expressions

A lambda expression is like a method: it provides a list of formal parameters and a body - an expression or block - expressed in terms of those parameters.

```
LambdaExpression:
  LambdaParameters -> LambdaBody
```

Lambda expressions are always poly expressions ([§15.2](15.2.md)).

It is a compile-time error if a lambda expression occurs in a program in someplace other than an assignment context ([§5.2](5.2.md)), an invocation context ([§5.3](5.3.md)), or a casting context ([§5.5](5.5.md)).

Evaluation of a lambda expression produces an instance of a functional interface ([§9.8](9.8.md)). Lambda expression evaluation does *not* cause the execution of the expression's body; instead, this may occur at a later time when an appropriate method of the functional interface is invoked.

Here are some examples of lambda expressions:

```
() -> {}                // No parameters; result is void
() -> 42                // No parameters, expression body
() -> null              // No parameters, expression body
() -> { return 42; }    // No parameters, block body with return
() -> { System.gc(); }  // No parameters, void block body

() -> {                 // Complex block body with returns
  if (true) return 12;
  else {
    int result = 15;
    for (int i = 1; i < 10; i++)
      result *= i;
    return result;
  }
}

(int x) -> x+1              // Single declared-type parameter
(int x) -> { return x+1; }  // Single declared-type parameter
(x) -> x+1                  // Single inferred-type parameter
x -> x+1                    // Parentheses optional for
                            // single inferred-type parameter

(String s) -> s.length()      // Single declared-type parameter
(Thread t) -> { t.start(); }  // Single declared-type parameter
s -> s.length()               // Single inferred-type parameter
t -> { t.start(); }           // Single inferred-type parameter

(int x, int y) -> x+y  // Multiple declared-type parameters
(x, y) -> x+y          // Multiple inferred-type parameters
(x, int y) -> x+y    // Illegal: can't mix inferred and declared types
(x, final y) -> x+y  // Illegal: no modifiers with inferred types
```

This syntax has the advantage of minimizing bracket noise around simple lambda expressions, which is especially beneficial when a lambda expression is an argument to a method, or when the body is another lambda expression. It also clearly distinguishes between its expression and statement forms, which avoids ambiguities or over-reliance on '`;`' tokens. When some extra bracketing is needed to visually distinguish either the full lambda expression or its body expression, parentheses are naturally supported (just as in other cases in which operator precedence is unclear).

The syntax has some parsing challenges. The Java programming language has always required arbitrary lookahead to distinguish between types and expressions after a '`(`' token: what follows may be a cast or a parenthesized expression. This was made worse when generics reused the binary operators '`<`' and '`>`' in types. Lambda expressions introduce a new possibility: the tokens following '`(`' may describe a type, an expression, or a lambda parameter list. Some tokens immediately indicate a parameter list (annotations, `final`); in other cases there are certain patterns that must be interpreted as parameter lists (two names in a row, a '`,`' not nested inside of '`<`' and '`>`'); and sometimes, the decision cannot be made until a '`->`' is encountered after a '`)`'. The simplest way to think of how this might be efficiently parsed is with a state machine: each state represents a subset of possible interpretations (type, expression, or parameters), and when the machine transitions to a state in which the set is a singleton, the parser knows which case it is. This does not map very elegantly to a fixed-lookahead grammar, however.

There is no special nullary form: a lambda expression with zero arguments is expressed as `() -> ...`. The obvious special-case syntax, `-> ...`, does not work because it introduces an ambiguity between argument lists and casts: `(x) -> ...`.

Lambda expressions cannot declare type parameters. While it would make sense semantically to do so, the natural syntax (preceding the parameter list with a type parameter list) introduces messy ambiguities. For example, consider:

```
foo( (x) < y , z > (w) -> v )
```

This could be an invocation of `foo` with one argument (a generic lambda cast to type `x`), or it could be an invocation of `foo` with two arguments, both the results of comparisons, the second comparing `z` with a lambda expression. (Strictly speaking, a lambda expression is meaningless as an operand to the relational operator `>`, but that is a tenuous assumption on which to build the grammar.)

There is a precedent for ambiguity resolution involving casts, which essentially prohibits the use of `-` and `+` following a non-primitive cast ([§15.15](15.15.md)), but to extend that approach to generic lambdas would involve invasive changes to the grammar.

**Sections:**

- [15.27.1. Lambda Parameters](15.27.1.md)
- [15.27.2. Lambda Body](15.27.2.md)
- [15.27.3. Type of a Lambda Expression](15.27.3.md)
- [15.27.4. Run-Time Evaluation of Lambda Expressions](15.27.4.md)
