Sub topic 11
Using `var` in Lambda Parameters
Assume you know Java lambda expressions, functional interfaces, and local variable type inference. This chapter focuses on a special use of var: naming the types of parameters inside a lambda expression.
Why use `var` in a lambda?
A lambda expression usually gets its parameter types from a target functional interface. A functional interface is an interface with one abstract method, such as BiFunction<Integer, Integer, Integer>.
import java.util.function.BiFunction;
public class LambdaTypes {
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> add =
(a, b) -> a + b;
System.out.println(add.apply(4, 5)); // 9
}
}The compiler knows that a and b are Integer values because BiFunction supplies the target type.
You can write the same lambda with var:
import java.util.function.BiFunction;
public class VarLambda {
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> add =
(var a, var b) -> a + b;
System.out.println(add.apply(4, 5)); // 9
}
}Here, var does not create dynamic typing. The target interface still determines that both parameters are Integer.
The all-or-nothing rule
When a lambda has multiple parameters, you must use the same style for every parameter. You may use var for all parameters, or use no type keyword for any parameter.
| Parameter style | Example | Valid? |
|---|---|---|
| Implicit types | `(a, b) -> a + b` | Yes |
| `var` for every parameter | `(var a, var b) -> a + b` | Yes |
| Explicit types for every parameter | `(int a, int b) -> a + b` | Yes |
| Mixed `var` and implicit | `(var a, b) -> a + b` | No |
| Mixed `var` and explicit | `(var a, int b) -> a + b` | No |
The compiler rejects mixed forms because they use inconsistent parameter declarations.
import java.util.function.BiFunction;
public class InvalidLambda {
public static void main(String[] args) {
// Does not compile:
// BiFunction<Integer, Integer, Integer> sum =
// (var a, b) -> a + b;
// Does not compile:
// BiFunction<Integer, Integer, Integer> other =
// (var a, Integer b) -> a + b;
}
}Why `var` can be useful
The main reason to use var in a lambda is to attach an annotation to a parameter. Java does not allow an annotation on an implicitly typed lambda parameter. With var, you can write the annotation while retaining target-type inference.
import java.util.function.Predicate;
public class AnnotatedLambda {
public static void main(String[] args) {
Predicate<String> nonEmpty =
(@Deprecated var text) -> !text.isEmpty();
System.out.println(nonEmpty.test("Java")); // true
}
}@Deprecated is used here only to demonstrate syntax. In real code, use an annotation that has a meaningful purpose in your project.
A lambda parameter using var still receives its type from the target interface:
flowchart TD
A["Target functional interface"] --> B["Parameter types"]
B --> C["Lambda using var"]
C --> D["Compiler checks the lambda body"]
D --> E["Generated code remains statically typed"]Common errors
A lambda cannot use var without a target type. The compiler must know which functional interface describes the lambda.
public class NoLambdaTarget {
public static void main(String[] args) {
// Does not compile:
// var add = (var a, var b) -> a + b;
}
}The problem is not specifically the word var. A lambda expression needs a target type even without it:
import java.util.function.BiFunction;
public class LambdaTarget {
public static void main(String[] args) {
var add = (BiFunction<Integer, Integer, Integer>)
((a, b) -> a + b);
System.out.println(add.apply(2, 3)); // 5
}
}In most code, declaring the functional interface directly is clearer.
Exercise
Is (var left, right) -> left + right valid when assigned to a BiFunction<Integer, Integer, Integer>? If not, how can you correct it?
Show answer
No. Lambda parameters cannot mix var and implicit declarations. Write (var left, var right) -> left + right, or write (left, right) -> left + right.