The first Lambda expression
Before Lambda appeared, if we needed to write a multithread, we might need the following method:
Runnable runnable = new Runnable() { @Override public void run() { System.out.println("Hello runnable"); } }; ... thread.start();The above example will be much easier if you change to using Lambda:
Runnable noArgs = ()->System.out.println("Hello Lambda!~");... thread.start();A Lambda expression is an anonymous function that passes behavior like data. The expression uses -> to separate the parameters from the body, -> to precede the parameter part, and then this is the body part.
Other forms of Lambda
BinaryOperator<Long> add = (x,y)->x+y; //OR BinaryOperator<Long> add = (Long x,Long y)->x+y; //OR BinaryOperator<Long> add = (Long x,Long y)->{ x+y; };The above are all the forms of Lambda expressions.
Then the question is:
What does BinaryOperator<Long> add = (Long x,Long y)->x+y; mean?
This line of code does not talk about adding numbers, but creates a function to calculate the result of adding two numbers. The type of add is BinaryOperator<Long> , which is not the sum of two numbers, but the code that adds two numbers. The following example shows how to use this variable:
BinaryOperator<Long> add = (Long x,Long y)->x+y; Long res = add.apply(3L, 4L); System.out.println("res="+res);//Output: res=7 Function Interface
A function interface is an interface with only one abstract method, used as a type of Lambda expression.
For example, Runnable is a function interface.
Create a new function interface:
public interface AddOperator<T,D> { long add(T one,D two);}use:
AddOperator<Long,Long> addOperator = ( x, y)->x +y; System.err.println("Custom function listener res= "+addOperator.add(34L, 65L)); Target Type
The target type refers to the type of the context in which the Lambda expression is located. For example, assign a Lambda expression to a local variable, or pass it to a method as a parameter. The type of the local variable or method parameter is the target type of the Lambda expression.
Final
If we reference the local variable in the method in the anonymous inner class, this requires that the local variable is final.
In Lambda we do not need to declare the referenced external local variable as final, but the variable can only be assigned once.
In the following example, if we remove the comments and assign the name again, it will not be compiled and an error message will be displayed: local variables referenced from a Lambda expression must be final or effectively final.
The local variable referenced by the Lambda expression must be final or a factual final.
String name = getUserName();//name="hi";button.addActionListener(event->System.out.println("name="+name)); Type inference
The type inference of Lambda expressions is an extension of the target type inference introduced in Java7.
Type inference in java7:
List<String> list= new ArrayList<>();
In the above example, we did not specify the paradigm type of ArrayList, but instead inferred the paradigm type of ArrayList based on the type of list.
For example: AddOperator<Long,Long> addOperator = ( x, y)->x +y;
Through this article, I hope it can help you learn and understand this part of the knowledge. Thank you for your support for this website!