Java 11 (LTS) · Topic 5 of 6
var in lambda parameters
Definition​
Java 11 lets you write var for lambda parameters. It does not change behaviour, but it lets you add annotations such as @NonNull to a parameter without writing out its type.
Example code​
Using var for lambda parameters
import java.util.List;
import java.util.function.BiFunction;
class Demo {
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> add = (var a, var b) -> a + b;
System.out.println(add.apply(2, 3));
List<String> names = List.of("asha", "ravi");
names.forEach((var name) -> System.out.println(name.toUpperCase()));
}
}
// Output:
// 5
// ASHA
// RAVI
Finished this page? Track your progress (saved only in your browser).