English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java 8Lambda Expressions in Java

Lambda expressions are in Java 8introduced, hailed as Java 8of the most powerful features. Lambda expressions help with functional programming and greatly simplify the development process.

Syntax

The syntax of lambda expressions is characterized by the following.

parameter -> expression body

The following are important features of lambda expressions.

  • Optional type declaration-No need to declare the type of parameters. The compiler can infer the type from the parameter value.

  • Optional parentheses around parameters-No need to declare a single parameter in parentheses. For multiple parameters, parentheses are required.

  • Optional braces-If the body contains a single statement, no braces are required in the expression body.

  • Optional return keyword-If the body has a single expression that returns a value, the compiler will automatically return the value. Braces are required to indicate that an expression returns a value.

Lambda Expression Example

Create the following Java program using any editor of your choice (for example, C:\> JAVA).

Java8Tester.java

public class Java8Tester {
   public static void main(String args[]) {
      Java8Tester tester = new Java8Tester();
      //with type declaration
      MathOperation addition = (int a, int b) -> a + b;
      //no type declaration
      MathOperation subtraction = (a, b) -> a - b;
      //with return statement and curly braces
      MathOperation multiplication = (int a, int b) -> { return a * b; };
      //no return statement, no curly braces
      MathOperation division = (int a, int b) -> a / b;
      System.out.println("10 + 5 = " + tester.operate(10, 5, addition));
      System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
      System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
      System.out.println("10 / 5 = " + tester.operate(10, 5, division));
      //without parentheses
      GreetingService greetService1 = message ->
      System.out.println("Hello " + message);
      //with parentheses
      GreetingService greetService2 = (message) ->
      System.out.println("Hello " + message);
      greetService1.sayMessage("Mahesh");
      greetService2.sayMessage("Suresh");
   }
   interface MathOperation {
      int operation(int a, int b);
   }
   interface GreetingService {
      void sayMessage(String message);
   }
   private int operate(int a, int b, MathOperation mathOperation) {
      return mathOperation.operation(a, b);
   }
}

Verify the result

UsingjavacThe compiler compiles the class as follows:

C:\JAVA>javac Java8Tester.java

Now, run Java as follows8Tester.

C:\JAVA>java Java8Tester

Output Result

10 + 5 = 15
10 - 5 = 5
10 x 5 = 50
10 / 5 = 2
Hello Mahesh
Hello Suresh

The following are the key points to consider in the above example.

  • Lambda expressions are mainly used to define inline implementations of functional interfaces, that is, interfaces that have only one method. In the above example, we used various types of lambda expressions to define the operation methods of the MathOperation interface. Then, we defined the implementation of the sayMessage method of GreetingService.

  • Lambda expressions eliminate the need for anonymous classes and provide Java with very simple and powerful functional programming capabilities.

Scope

Using lambda expressions, you can refer to any final variable or effectively final variable (assigned only once). If a variable is assigned a value a second time, the lambda expression will cause a compilation error.

Scope Example

Create the following Java program using any editor of your choice (for example, C:\> JAVA).

Java8Tester.java

public class Java8Tester {
   final static String salutation = "Hello! ";
   public static void main(String args[]) {
      GreetingService greetService1 = message ->
      System.out.println(salutation + message);
      greetService1.sayMessage("Mahesh");
   }
   interface GreetingService {
      void sayMessage(String message);
   }
}

Verify the result

Use it as followsjavacThe compiler compiles the class.

C:\JAVA>javac Java8Tester.java

Now, run Java as follows8Tester.

C:\JAVA>java Java8Tester

Output Result

Hello! Mahesh