Lambda An expression
form: parameter, arrow (-), expression
Parameter type to write, if you can deduce it can not write
Only one argument can omit parentheses
No arguments to write empty brackets ()
* If the branch returns a value, then all branches will return a value, otherwise it is illegal
function interface: interface with only one abstract method (can be marked with @functionalinterface annotations)
For example, you can do this in Javax.swing.Timer.
Timer T = new timer (+, event-
{
...
});
The second parameter of this constructor requires a ActionListener instance, ActionListener
is an interface that has only one method, so you can use a lambda expression.
Method reference
Object::instancemethod
Class::staticmethod
Class::instancemethod
Such as: Event-System.out.println (event) is equivalent to System.out:: println
Math::p ow equivalent to (x, y)-Math.pow (x, y)
In the third case, the first parameter becomes the target of the method, such as: String::comparetoignorecase equivalent to (x, y), X.comparetoignorecase (y)
There is also the ability to use this and super parameters;
Super:: Instancemethod
Constructor Reference:
X-New int[x] equivalent to Int[]::new
Person x = New person (...) equivalent to Person::new
When there are multiple constructors, the compiler infers from the context
Learn the lambda essay on the Java Core Technology volume