Lambda expressions 8 new features of the Java entry

Lambda expressions is an important update Java 8, the majority of developers is also a long-awaited new features. Lambda support block as a parameter, Lambda expressions allow the use of more compact code to create an instance of an abstract method only one interface (this interface is called functional interface).

So Lambda expressions in the end is how to write it, we first look at its syntax. In fact, it consists of three parts.

l parameter list. Parameter list allows omitting the parameter type. If only one parameter in the parameter list, the parameter list even parentheses may be omitted.
l arrow (->). English must be formed by drawing lines and greater than symbol.

l code block. If the block of code contains only one statement, Lambda expressions allow braces block of code is omitted, then this statement do not use curly braces indicate the end of the statement. Lambda expressions only one return statement, or even omit the return keyword. Lambda expressions return value is needed, and it is only a block of code is omitted return statement, Lambda expressions automatically returns the value of this statement.

Here we illustrate several simplified wording Lambda expressions.   

package com.itheima;

public class
Demo {

    //
调用该方法需要Eatable对象

    public void eat(Eatable e)
{

        System.out.println(e);

        e.taste();

    }

    //
调用该方法需要Flyable对象

    public void drive(Flyable f)
{

        System.out.println("我正在驾驶:"
+  f);

        f.fly("【碧空如洗的晴日】");

    }

    //
调用该方法需要Addable对象

    public void test(Addable add)
{

        System.out.println("5与3的和为:"
+  add.add(5,
3));

    }

    public static void main(String[] args)
{

        Demo demo=
new  Demo();

        //
Lambda表达式的代码块只有一条语句,可以省略花括号。

        demo.eat(()
->
System.out.println("苹果的味道不错!"));

        //
Lambda表达式的形参列表只有一个形参,省略圆括号

        demo.drive(weather ->
{

            System.out.println("今天天气是:"
+  weather);

            System.out.println("直升机飞行平稳");

        });

        //
Lambda表达式的代码块只有一条语句,省略花括号

        //
代码块中只有一条语句,即使该表达式需要返回值,也可以省略return关键字。

        demo.test((a,
b)
->
a +
b);

    }
interface Eatable {

    void taste();

}

interface Flyable {

    void fly(String weather);

}

interface Addable {

    int add(int a,
int b);

}

Above the first paragraph of bold program codes using a Lambda expression is equivalent method without anonymous parameter due to the Lambda expressions one-line code, the code block braces can be omitted; second segment in bold Lambda expressions using the code word corresponding to the anonymous method with only one parameter, the parameter list since only a Lambda expression of the parameter, the parameter is omitted parentheses list; bold Lambda third segment code expression of only one row block statement, the statement that the return value of the line as the return value of the code block.

We found that more than three sections of code that calls a method in bold are need to pass Eatable type parameters, Flyable type parameter, Addable parameters, but in fact we just passed Lambda expressions, but above normal procedures can compile and run , indicating Lambda expressions will actually be treated as an object of "any type", in the end what type of objects as needed, depending on the needs of the operating environment, etc. we will detail what Lambda expressions are treated as objects .

Guess you like

Origin blog.51cto.com/14473726/2440355