Feature
- Allow lambda expression parameter as a function of the method
- lambda expression more concise
feature
- Optional type declaration: do not need to declare the parameter type, the compiler can be unified identification parameter value.
- The optional parameters in parentheses: a need to define the parameters in parentheses, but a number of parameters need to be defined parentheses.
- Optional braces: if the body contains a statement, you do not need to use braces.
- Optional return keyword: 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
grammar
- lambda expression consists of two parts [parameter list] -> [realize]
(parameters)->express 或 (parameters)->{express}
Expression syntax examples
@Test public void test1 () { // 1. line of code the Runnable runnable1 = () -> System.out.println ( "Hello" ); the Runnable runnable2 = () -> { System.out.println ( "Hello" ) ; }; // 2 lines of code the Runnable runnable3 = () -> { System.out.println ( "Hello" ); System.out.println ( "Hello" ); System.out.println ( "Hello" ) ; }; // 3. a parameter type may be ignored x-> the System.out.println(x); (x) -> System.out.println (X); (String X) -> System.out.println (X); // or more parameters of type 4.2 can be ignored (x, y) -> System.out.println (X + Y); (X String, String Y) -> System.out.println (X + Y); // 5. The return a line of code () -> 5 ;
// 6 has multiple return lines () - > { int A = 10 ; return A; } }