Introduction
Java 8 (also known as jdk 1.8) is a major version of Java language development. There are many new features in java8. This article focuses on Lambda expressions. Lambda expressions, also known as closures, are the most important new feature driving the java 8 release. Lambda allows functions to be passed as parameters to a method.
grammar
The complete Lambda expression consists of three parts: parameter list, arrow and declaration statement;
(Type1 param1, Type2 param2, ..., TypeN paramN) -> { statment1; statment2; ...... return statmentM;}
The following are important features of Lambda expressions:
-
Optional type declaration: it is not necessary to declare parameter types, and the compiler can identify parameter values uniformly. (param1,param2, ..., paramN) -> { statment1; statment2; ...... return statmentM;}
-
Optional parameter parentheses: one parameter does not need to define parentheses, but multiple parameters need to define parentheses. param1 -> { statment1; statment2; ...... return statmentM;}
-
Optional braces: if the body contains a statement, you do not need to use braces. (param1,param2, ..., paramN) -> statment1
-
Optional return key: if the body has only one expression return value, the compiler will automatically return the value. The brace needs to indicate that the expression returned a value. param1 -> statment1
Lambda expression instance
A simple example of a Lambda expression:
// 1. No parameter required, return value is 5 () -> 5 // 2. Receive a parameter (numeric type) and return 2 times its value x -> 2 * x // 3. Accept 2 parameters (numbers) and return their difference value (x, y) -> x – y // 4. Receive 2 int integers and return their sum (int x, int y) -> x + y // 5. Accept a string object and print it on the console without returning any value (it looks like returning void) (String s) -> System.out.print(s)
Now we implement it in java code:
package com.adanblog.demo; /** * Lambda Expression example * @author adanblog */ public class LambdaTest { public static void main(String[] args) { // 1. No parameter is required, return value is 5, () - > 5 Fun1 f=()->5; System.out.println("Back to 5:"+f.test()); // 2. Receive a parameter (numeric type) and return 2 times its value Fun2 f2=x -> 2 * x; System.out.println("5 * 2 = "+f2.test(5)); // 3. Accept 2 parameters (numbers) and return their difference value Fun3 f3=(x, y) -> x - y; System.out.println("5 - 3 = "+f3.test(5,3)); // 4. Receive 2 int integers and return their sum Fun4 f4=(int x, int y) -> x + y; System.out.println("5 + 3 = "+f4.test(5,3)); // 5. Accept a string object and print it on the console without returning any value (it looks like returning void) Fun5 f5=(String s) -> System.out.print(s); f5.test("We're here for parameters lamdba"); } interface Fun1 { int test(); } interface Fun2 { int test(int a); } interface Fun3 { int test(int a,int b); } interface Fun4 { int test(int a,int b); } interface Fun5 { void test(String str); } }
Print after executing the main method:
Return to 5:5 5 * 2 = 10 5 - 3 = 2 5 + 3 = 8 We're here to parameter lamdba
Variable scope
Lambda expressions can only refer to outer local variables marked with final, that is to say, local variables defined outside the domain cannot be modified inside lambda, otherwise they will be compiled as. Code:
package com.adanblog.demo; /** * Lambda Example of expression variable scope * @author adanblog */ public class LambdaTest2 { final static String salutation = "Hello! "; public static void main(String[] args) { GreetingService gs=(mes)->System.out.println(salutation+mes); gs.sayMessage("I am Lambda Test!"); } interface GreetingService { void sayMessage(String message); } }
Operation output:
Hello! I'm Lambda's test!
We can also directly access the outer local variables in the lamdba expression:
package com.adanblog.demo; /** * Lambda Example of expression variable scope * @author adanblog * */ public class LambdaTest3 { public static void main(String[] args) { int num=2; Fun c=(par)->System.out.println(String.valueOf(num+par)); c.test(5);//Output is 7 } interface Fun{ void test(int i); } }
The local variable of Lambda expression may not be declared as final, but it must not be modified by the following code (that is, the implicit semantics with final) In the above example, changing the value of the variable num will result in an error
package com.adanblog.demo; /** * Lambda Example of expression variable scope * @author adanblog * */ public class LambdaTest3 { public static void main(String[] args) { int num=2; Fun c=(par)->System.out.println(String.valueOf(num+par)); c.test(5);//Output is 7 //If you change the variable value here, the compiler will report an error: local variable num defined in an embedding scope must be final or effective final num=5; } interface Fun{ void test(int i); } }
In a lambda expression, it is not allowed to declare a parameter or local variable with the same name as the local variable.
int num=2; Fun c=(num)->System.out.println(String.valueOf(num));//Compile and report errors
About Adan blog The above content is just one family's opinion. Due to limited personal ability, there are inevitably omissions and mistakes. If you find a bug or have better suggestions, you are welcome to comment and correct. Thank you very much. Here's Adan's personal blog, which records all the blogs in study and work. Welcome to visit.
Personal blog: http://www.adanblog.com/
GitHub: https://github.com/adanblog/