What is the data type of a lambda expression in Java?


The lambda expressions have a very simple, precise syntax and provide flexibility to specify the datatypes for the function parameters. Its return type is a parameter -> expression body to understand the syntax, we can divide it into three parts.

  • Parameters : These are function method parameters and match with the signature of a function defined in the functional interface. Defining the data-type of parameters is optional but the number of parameters can match with the defined signatures in the interface.
  • Expression Body : This is either a single statement or collection of statements that represent the function definition. Defining the data-type for return object is optional.
  • -> : This represents the lambda expression operator.

Example

interface Hello {
   String sayHello(String name);
}
public class LambdaExpressionTest {
   public static void main(String args[]) {
      Hello hello = (message) -> {
         String str1 = "Hello ";
         String str2 = str1 + message;
         return str2;
      };
       System.out.println(hello.sayHello("Tutorials Point"));
   }
}

Output

Hello Tutorials Point
raja
Published on 02-Dec-2019 09:14:51
Advertisements