Latest web development tutorials

Java 8 Lambda expressions

Java 8 new features Java 8 new features


Lambda expressions can also be called a closure, which is to promote the most important new features of Java 8 release.

Lambda allowed to function as a parameter of a method (function passed as a parameter into the method).

Lambda expressions can make use of the code becomes more simple and compact.

grammar

Lambda expression syntax is as follows:

(parameters) -> expression
或
(parameters) ->{ statements; }

The following are important characteristics of lambda expressions:

  • Optional type declaration: no need to declare parameter types, the compiler can be unified identification parameter value.
  • The optional parameters in parentheses: a parameter defined without parentheses, but a number of parameters need to be defined parentheses.
  • Optional braces: if the body contains a statement that you do not need braces.
  • Optional return keywords: If the subject is only one expression that returns a value, the compiler will automatically return value, braces need to specify clear expression that returns a value.

Lambda expressions Examples

In Java8Tester.java file enter the following code:

public class Java8Tester {
   public static void main(String args[]){
      Java8Tester tester = new Java8Tester();
		
      // 类型声明
      MathOperation addition = (int a, int b) -> a + b;
		
      // 不用类型声明
      MathOperation subtraction = (a, b) -> a - b;
		
      // 大括号中的返回语句
      MathOperation multiplication = (int a, int b) -> { return a * b; };
		
      // 没有大括号及返回语句
      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));
		
      // 不用括号
      GreetingService greetService1 = message ->
      System.out.println("Hello " + message);
		
      // 用括号
      GreetingService greetService2 = (message) ->
      System.out.println("Hello " + message);
		
      greetService1.sayMessage("w3big");
      greetService2.sayMessage("Google");
   }
	
   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);
   }
}

Implementation of the above script, output is:

$ javac Java8Tester.java 
$ java Java8Tester
10 + 5 = 15
10 - 5 = 5
10 x 5 = 50
10 / 5 = 2
Hello w3big
Hello Google

Use Lambda expressions to note the following points:

  • Lambda expressions are mainly used in the method definition line interfaces to perform, for example, a simple way to interface. In the above example, we use various types of Lambda expressions to define methods MathOperation interface. Then we define the execution sayMessage.
  • Lambda expressions anonymous method eliminates the use of cumbersome and given Java function of simple but powerful programming capabilities.

Variable Scope

lambda expressions can only refer to final or final local variables, which means that you can not modify the definition of extraterritorial variables lambda inside, otherwise compilation errors.

In Java8Tester.java file enter the following code:

public class Java8Tester {

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

Implementation of the above script, output is:

$ javac Java8Tester.java 
$ java Java8Tester
Hello! w3big

Java 8 new features Java 8 new features