Open In App

Java 11 Lambda Features with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

In essence, lambda expressions express examples of functional interfaces (a functional interface is an interface with a single abstract method). Java.lang. Runnable is an excellent example. Lambda expressions, which also implement functional interfaces, implement the lone abstract function. In Java 11, lambda expressions were added, and they offer the following functionalities.

The var keyword is supported in lambda expressions in Java 11:

The var keyword was added in Java 10. Using the var keyword, variables can be declared and defined without their data types being specified. The compiler determines the type based on the assigned data type. A var declaration is not permitted in a lambda expression. 

Declare variables with type in a lambda expression:

 StringOp s = (String left,String  right) -> left + right; 

Similarly, as shown below, You Can Declare Without Type:

StringOp s = (left, right) -> left + right; 

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
interface StringOp {
    String concat(String left, String right);
}
 
class GFG {
    public static void main(String[] args)
    {
        StringOp s
            = (left, right) -> left + right;
        String op = s.concat("Hello", " World");
        System.out.println(op);
    }
}


Output:

Hello World

Using the var keyword, variables can be declared and defined without their data types being specified. The compiler determines the type based on the assigned data type. A var declaration is not permitted in a lambda expression.

Java: The -source 10 compiler does not support the var syntax in implicit lambdas (use -source 11 or higher to enable var syntax in implicit lambdas)

StringOperation str = (var left,var  right) -> left + right; 

With java11, It supports lambda expression with the var keyword

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
interface StringOper {
    String concat(String left, String right);
}
 
class GFG {
    public static void main(String[] args)
    {
          // using var keyword
        StringOper s  = (var left, var right) -> left + right;          
        String op = s.concat("Hello", " World");
        System.out.println(op);
    }
}


Output:

Hello World

Java11 Var In Lambdas: Rules To Follow

  • Lambda Despite there being only one parameter, var-declared parameters need to be surrounded in parenthesis ().
  • It is not permitted to combine var with other non-var parameters. It can only var to var.
  • It is likewise prohibited to use var for one parameter while skipping another.

Java11 var keyword limitations

Using a lambda expression Either use var to apply all parameters or don’t.

(var first,var second) -> first + second; – valid and works (var first, second) -> first + second; – Invalid and throws following error

Java: Invalid lambda parameter declaration (cannot mix ‘var’ and implicitly-typed parameters)

You can not mix var with explicit types and the below code throws java: invalid lambda parameter declaration (cannot mix ‘var’ and explicitly-typed parameters).

(var left,String right) -> left + right; 

We have to use parenthesis if the var keyword is used.



Last Updated : 28 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads