Java 8 lambda expression

Source: Internet
Author: User
Tags event listener

The lambda expression in Java 8 is a function interface, which is an interface with only one abstract method.

In Java, passing a behavior is accomplished by passing an object that represents a certain behavior, for example, to register an event listener for a button:

Button.addactionlistener (new  ActionListener () {    @Override    publicvoid actionperformed (ActionEvent e) {        System.out.println ("button clicked");}    );

An actionperformed behavior is passed through an anonymous inner class. However, this way of passing behavior has to add some cumbersome boilerplate code, which is not easy enough.

In Java 8, you can write this:

Button.addactionlistener (Event-System.out.println ("button clicked"));

In fact, the LAMDBA expression type is a function interface, but in Java 8, the compiler can infer various kinds of information about the behavior of the lambda expression based on the context in which it is located.

A core set of function interfaces is provided in the JDK that can be used to pass the behavior.

A lambda expression is an anonymous method that passes the behavior as if it were data.

Java 8 lambda expression

Related Article

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.