Java lambda expressions (1)

Source: Internet
Author: User

Java lambda expressions (1)
Basic usage of lambda expressions

The callback and the lambda expression of Java 8 demonstrate the basic purpose of the lambda expression of Java 8: to complete the original intent of the callback-code parameterization.

Callback: To put it simply, if your method requires a method of a class at the underlying layer of override or JDK and you have never called it yourself, this method is called back. For example, the init (), start (), stop (), and destroy () defined by the Applet; the drawing method is paint (Graphics), update (Graphics), and repaint (); java. lang. runnable run (); ActionListener actionreceivmed (ActionEvent e) in the GUI )...

Before Java 8, the callback code was provided by the Anonymous class. For the complete code, see the callback and Java 8 lambda expressions.

        s.register(new IClient(){             @Override public void callback(int i){                 System.out.println("==" + i + "0%");             }        });
Now you can:

        IClient listener1=(i)->{System.out.println("+" + i + "0%");};        s.register(listener1);
        s.register((i)->{System.out.println("++" + i + "0%");});
Obviously, using the lambda expression to provide callback code is more concise than the Anonymous class and more concise than the Client implements IClient.

The Lambda expression consists of three parts:(Parameter list), arrow (->), {A code block}.


Because IClient is an interface with only one abstract method, the compiler recognizes "a Lambda expression" as a method body for the unique abstract method. Since the only abstract method is

Public void callback (int I );

Then "a Lambda expression" can be as simple as possible.
IClient listener1 = (int I)-> {System. out. println ("+" + I + "0%") ;}; // The method name is not required. The method body must be provided for callback. No. The third part of the lambda expression cannot accommodate the method name.

Listener1 = (I)-> {/**/}; // If callback (int I, String s), the compiler derives the type from (ii, str)

Listener1 = I-> {}; // only a single form parameter can be omitted.

In other cases, follow the principle of least coding and try it yourself. For example

The only abstract method is int getX (). Its Lambda expression is ()-> {Return 0 ;}Or ()-> 0; // Yqj2065 also wants to omit the brackets, "-> 0". The only abstract method is int add (int I, int j ). the Lambda expression is (I, j)-> {return I + j;} or (I, j)-> I + j;

Why can function interfaces be written?

IClient listener1 = a Lambda expression; s. register (a Lambda expression );

Because IClient is an interface with only one abstract method-function interface ).

In the first case, "IClient listener1 = a Lambda expression;", the compiler interprets a Lambda expression as providing code for only abstract methods of IClient. Although IClient "is a" Object, you can

Object obj = listener1;

However,

Object obj = a Lambda expression;

It seems unreliable, because "A Lambda expression" provides code for only abstract methods?

Object obj = (IClient) a Lambda expression; // OK

In 2nd cases, the s. register (a Lambda expression), because register (IClient listener), the compiler knows that a Lambda expression provides code for only abstract methods of IClient.

package Lower;/** * @author yqj2065 */@FunctionalInterfacepublic interface IClient extends Runnable{    int add(int i,int j);          @Override  default public  void run(){};}
The function interface has only one abstract method. If IClient inherits Runnable and wants to @ FunctionalInterface, it must provide the method body to the inherited run () (Here we give an empty method body to count ).
Function interface. Generally, the default keyword is used in the interface to add method implementation. This is the interface's Default Method.

The default method appears in the function interface, which seems to be a reason. Generally, the default method is used in the interface. For details, refer to the tangled default method.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.