Java8 new features: Lambda Introduction and grammar

Original link: http://www.cnblogs.com/drl-blogs/p/10803013.html

What Lambda that?

Lambda is an anonymous function, we can put Lambda expressions it can be understood as a piece of code passed (pass code is the same as the data). You can write simpler, more flexible code. As a more compact style code, java language expressing ability has been improved

Lambda expression syntax

Lambda expressions introduced a new syntax elements and operators in the Java language. This operator is " -> ", the operator or operators is called Lambda arrow operator. It Lambda divided into two parts:
the left : specifies all the parameters required Lambda expressions
right : Lambda specified functions, i.e. Lambda be performed.

  • A syntax: no arguments and returns no value, Lambda body of only one statement

    Runnable r1=()->System.out.println(“Hello Lambda!”);

  • Two syntax: Lambda requires an argument

Consumer<String> fun=(args)->System.out.println(args);

  • Syntax III: Lambda when only one parameter, the parameter can be omitted parentheses

Consumer<String> fun=args->System.out.println(args);

  • Syntax four: Lambda requires two parameters, and return values

BinaryOperator <Long> BO = (X, Y) -> {
System.out.println ( "function implemented interface methods!");
Return X + Y;
}

  • Syntax five: Lambda body only when a statement, return with braces may be omitted

BinaryOperator<Long> bo=(x,y)->x+y;

  • Six syntax: type of data can be omitted, as inferred by the compiler, called "type inference"

Reproduced in: https: //www.cnblogs.com/drl-blogs/p/10803013.html

Guess you like

Origin blog.csdn.net/weixin_30215465/article/details/94879779