Local-Variable Syntax for Lambda Parameters in 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
(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
varfor 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
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
}
}
[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.
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));
}
}
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
5. Important Points and Best Practices
- Use
varin lambdas only when you need an annotation orfinal; otherwise implicit parameters are shorter. - Do not mix styles within one lambda.
varis 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
varfor lambda parameters. - The main benefit is adding annotations and
finalto 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
Frequently Asked Questions (FAQ)
Q1. Can var be used in lambda parameters?
Yes, since Java 11 (JEP 323). For example, (var x, var y) -> x * y.
Q2. What is the benefit of var in lambda parameters?
It allows annotations and modifiers such as final on lambda parameters while keeping the types inferred.
Q3. Can I mix var with other parameter styles?
No. A lambda must use var for every parameter or for none; mixing styles is a compile error.
Q4. Does var change performance?
No. It only affects the source code. The compiler infers the same types and produces the same bytecode.