Topic 2: Basic syntax

Basic elements:

The syntax of a lambda expression parameter list, the arrow symbol -> function body composition;

parameter list:

Since the target type (function interface) is already "knows" in the form of a lambda expression parameter types, so we do not need to repeat the known type. That is, the parameter type of the lambda expression can be derived from the target type ( parameter type is omitted ):

Comparator<String> c = (s1, s2) -> s1.compareToIgnoreCase(s2);

If lambda is only one parameter can be omitted parentheses (consider readability, retention as well).

FileFilter java = f -> f.getName().endsWith(".java");  

Function body:

It may be a function expression may be a code block;

expression:

    Expression is performed and then returns an execution result; if the expression is not the execution result, the interface type represented by type of method does not return. Use an expression as a function of the body, you do not need the extra semicolon is over.

If the target type of method does not return a value, it will only execute the function body .

Runnable t = ()-> System.out.println(12);

Block:

Block of code statements are executed sequentially, just like the method body in the same statement:

(1) return is used to return results or method invocations.

(2) break and continue in a loop can only be used

(3) If the function returns a value thereof, then each path inside the body of the function must return value ??

interface At{
     String a();
}

At a = ()->{
     System.out.println("aaaaaaaaaaa");
     return "aaaaaa";
};

Guess you like

Origin www.cnblogs.com/zyj-468161691/p/12213188.html