00:00

Local-Variable Syntax for Lambda Parameters in Java 11

Last updated: · Standard (Java 11)

Java 11 (JEP 323) allows var as the type of lambda parameters, as in (var x, var y) -> x + y. The types are still inferred exactly as before; the benefit is that a parameter now has a place for annotations and the final modifier.

This tutorial explains the motivation, the syntax rules and the compiler errors you get when the rules are broken.

1. Why Was This Feature Added?

Java 10 introduced var for local variables (JEP 286). Lambda parameters were left out, so implicitly typed parameters could not carry annotations such as @NonNull without writing the full type. JEP 323 makes the syntax consistent: var can be used wherever an implicitly typed lambda parameter is allowed.

2. Syntax and Rules

Rules.java
(var a, var b) -> a + b                      // allowed
(final var name) -> name.strip()             // allowed: modifier
(@Trimmed var s) -> s.strip()                // allowed: annotation

(var a, b) -> a + b                          // error: cannot mix var and implicit
(var a, String b) -> a + b                   // error: cannot mix var and explicit
var a -> a                                   // error: var needs parentheses
  • Use var for all parameters of a lambda or for none.
  • Parentheses are required, even for a single parameter.
  • The inferred type is the same as with implicit parameters; nothing changes at run time.

3. Practical Examples

Example 1: Annotations and final on lambda parameters

VarInLambda.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

public class VarInLambda {
    // A simple marker annotation that can be placed on parameters
    @Retention(RetentionPolicy.SOURCE)
    @Target(ElementType.PARAMETER)
    @interface Trimmed { }

    public static void main(String[] args) {
        List<String> names = List.of(" asha ", "ravi", " meera");

        // Implicit types (Java 8)
        System.out.println(names.stream().map(s -> s.strip()).collect(Collectors.toList()));

        // var (Java 11): same inferred type, but now annotations and final are allowed
        System.out.println(names.stream().map((@Trimmed var s) -> s.strip()).collect(Collectors.toList()));

        // final + var for every parameter of a two-parameter lambda
        BiFunction<Integer, Integer, Integer> area = (final var w, final var h) -> w * h;
        System.out.println("area = " + area.apply(4, 5));

        // Not allowed (compile errors):
        //   (var a, b) -> a + b          mixing var and implicit
        //   (var a, int b) -> a + b      mixing var and explicit types
        //   var a -> a                   var needs parentheses
    }
}
Output
[asha, ravi, meera]
[asha, ravi, meera]
area = 20

@Trimmed is a simple annotation declared in the example. Real projects use annotations from validation or null-checking tools in the same position.

Example 2: The all-or-nothing rule

This file is meant to fail. The compiler output shows the exact error message from javac --release 11.

MixingVar.java
import java.util.function.BinaryOperator;

public class MixingVar {
    public static void main(String[] args) {
        BinaryOperator<Integer> ok = (var a, var b) -> a + b;   // all var: fine
        BinaryOperator<Integer> bad = (var a, b) -> a + b;      // var mixed with implicit
        System.out.println(ok.apply(1, 2) + bad.apply(3, 4));
    }
}
Compiler output
MixingVar.java:6: error: invalid lambda parameter declaration
        BinaryOperator<Integer> bad = (var a, b) -> a + b;      // var mixed with implicit
                                      ^
  (cannot mix 'var' and implicitly-typed parameters)
1 error

4. Three Ways to Declare Lambda Parameters

Implicit, explicit and var lambda parameters Implicit parameters have no type, explicit parameters name the type, and var parameters infer the type while allowing annotations and final. (a, b) implicit: no annotations (Integer a, Integer b) explicit: verbose (var a, var b) inferred + annotatable
Figure 1: var gives the brevity of implicit typing with the flexibility of explicit typing.

5. Important Points and Best Practices

  • Use var in lambdas only when you need an annotation or final; otherwise implicit parameters are shorter.
  • Do not mix styles within one lambda.
  • var is not a keyword but a reserved type name, so it can still be used as a variable name.

6. Summary

  • Java 11 (JEP 323) allows var for lambda parameters.
  • The main benefit is adding annotations and final to parameters.
  • All parameters must use var, or none, and parentheses are required.
  • Type inference and run-time behaviour do not change.

Related Tutorials

7. References