Introduction
Java 11 introduced a new feature called local-variable syntax for lambda parameters. This feature allows you to use the var
keyword in lambda expressions to declare the types of parameters, similar to how you can use var
for local variable type inference in methods.
This feature provides more flexibility in how you write lambda expressions and can improve readability, especially when you want to apply annotations to lambda parameters.
Key Points:
- Consistency Required: All parameters in a lambda must either use
var
or none at all; mixing is not allowed. - Annotation Support: Annotations can be used with
var
, but each annotated parameter must individually declarevar
. - Type Inference:
var
relies on type inference, meaning the parameter type is automatically determined based on context. - Readability: Using
var
can improve readability by reducing redundancy, but it should be used carefully to maintain code clarity.
Syntax
With the local-variable syntax for lambda parameters, you can use var
to declare the types of lambda parameters:
(var x, var y) -> x + y
- var: Indicates that the type of the parameter should be inferred.
- x, y: Lambda parameters whose types are inferred using
var
.
Example of Using Local-Variable Syntax for Lambda Parameters
Basic Example
Here’s a simple example demonstrating the use of local-variable syntax for lambda parameters:
import java.util.function.BiFunction; public class LambdaVarExample { public static void main(String[] args) { // Using local-variable syntax for lambda parameters BiFunction<Integer, Integer, Integer> add = (var a, var b) -> a + b; int sum = add.apply(5, 10); System.out.println("Sum: " + sum); } }
Output:
Sum: 15
Explanation:
- Local-Variable Syntax: The
var
keyword is used to declare the types of the lambda parametersa
andb
.- Type Inference: The types of the parameters are inferred based on the context, which in this case is
BiFunction<Integer, Integer, Integer>
.Annotations with Lambda Parameters
One of the main advantages of using the local-variable syntax for lambda parameters is the ability to apply annotations to lambda parameters. This was not possible with the traditional lambda parameter syntax.Example with Annotations:
import java.util.function.Function; public class LambdaAnnotationExample { public static void main(String[] args) { // Using annotations with lambda parameters Function<String, String> greet = (@Deprecated var name) -> "Hello, " + name; String greeting = greet.apply("Ananya"); System.out.println(greeting); } }
Output:
Hello, Ananya
Explanation:
- Annotations: The
@Deprecated
annotation is applied to the lambda parametername
, demonstrating how you can use annotations with lambda parameters when usingvar
.- Lambda Expression: The lambda expression uses
var
to declare the parameter, allowing annotations to be applied.Rules and Restrictions
Here are the simplified rules for using
var
in lambda parameters in Java, with examples:
- Same Declaration for All Parameters: If you use
var
for one parameter in a lambda expression, you must usevar
for all parameters. Mixingvar
with other forms of parameter declarations isn't allowed.(var x, var y) -> x + y // Valid: Both parameters use var (x, var y) -> x + y // Invalid: Mixing var with non-var
- Annotations with
var
: If you want to use annotations, each parameter must be declared withvar
, and the annotation should be placed directly before eachvar
parameter.(@NotNull var x, @NotNull var y) -> x + y // Valid: Both parameters have annotations and use var
- Type Inference: The
var
keyword automatically figures out the type of the parameter based on the context, so you don't need to specify it explicitly.(var x, var y) -> x + y // var infers the type of x and y from context
These simple rules help you use
var
effectively in lambda expressions.Example with Predicate:
import java.util.List; import java.util.function.Predicate; public class LambdaPredicateExample { public static void main(String[] args) { List<String> names = List.of("Ananya", "Rohit", "Lakshmi", "Vikram"); // Using var in lambda to filter names starting with 'A' Predicate<String> startsWithA = (var name) -> name.startsWith("A"); names.stream() .filter(startsWithA) .forEach(System.out::println); } }
Output:
Ananya
Explanation:
- Stream API: The example uses a stream to filter names starting with 'A' using a lambda expression with
var
.- Local-Variable Syntax: The
var
keyword provides type inference for the lambda parametername
.Conclusion
The local-variable syntax for lambda parameters in Java 11 enhances the flexibility and readability of lambda expressions. By allowing the use of
var
in lambda parameters, Java enables consistent syntax with local variable declarations and the application of annotations to lambda parameters.