Java8 new features of Lambda

Source: Internet
Author: User

First, what is lambda

In function programming, we often pass methods as parameters, so that they are not only structurally clear but also easier to organize a good code structure. For example JavaScript notation:

<script type= "Text/javascript" >        varfunction(callback) {        //  Call incoming function via function reference        callback ("callback")    }    //  call Test () and pass in the function implementation    Test (function(message) {        console.log (message)    })</script>

We see that a function is passedin when the test (callback) method is called, andcallback is a functional reference to the functions , then inside the test (callback) method An incoming function implementation can be invoked by callback this functional reference .

In the example above, the syntax of a function reference pass is simply illustrated by JavaScript. So is Java able to make that kind of notation?

Before JDK1.8, we were only able to do this by way of anonymous internal classes, as in the following example:

 Public classOldway { Public Static voidMain (string[] args) {dosth (NewCallback () {@Override Public voidCall () {System.out.println ("How to Anonymous internal classes");    }        }); }        Static voiddosth (Callback Callback) {callback.call (); }        InterfaceCallback {voidCall (); }}

Anonymous inner classes can be emulated to implement function reference passing like JavaScript, but we can do this without the hassle after JDK1.8, since 1.8 begins to provide a way to directly implement a function reference, which means you can also pass and call functions in Java through a function reference. That is the lambda of Java.

Ii. examples of Lambda

We know that Lambda is actually a way to support the implementation of the function through the reference to pass and control, then how to use it, directly to see an example:

 Public class lambdatest {    publicstaticvoid  main (string[] args) {        = (message ), {            System.out.println ("The message is:" + message);        Callback.call ("Hello Lambda");    }     Interface Callback {        void call (String message);}    }

First we define an interface callback, which contains a method call (String message).

In the main function, the "=" sign to the left is a reference, the right "(message)" means the method corresponding to the parameter "{}" is wrapped in a piece of output text information, that is, the callback interface declaration of call (String message) corresponding to the specific implementation.

Next we invoke its specific function call (String message) by referencing callback.

Let's simply rewrite it and pass the function as a parameter

 Public classLambdatest { Public Static voidMain (string[] args) {Callback Callback= (message),{System.out.println ("The message is:" +message); };//callback.call ("Hello Lambda");test (callback); }    Static voidTest (Callback Callback) {Callback.call ("Hello Lambda"); }    InterfaceCallback {voidCall (String message); }}

As you can see, the concrete implementation of the function in the test method can be passed as a parameter . Lambda lets Java support the use of functions as parameters in other methods.

Third, the wording of lambda

We can understand the wording of lambda as follows:

(parameter), {specific Code implementation};

1, if it is a parameter can not add "()"

parameter, {specific code implementation}

2, if the specific code implementation only a single line of statements, you can also do not add "{}"

parameter-specific Code implementation

3. If there is only one expression in the specific code that returns the value of the function, such as:

 return value

You can return the declaration without showing it, and it automatically returns

return value with Parameters

However, if the specific code is wrapped by "{}", then "return" must be added.

Iv. Summary

Lambda is one of the new features of Java8, which introduces the writing of functional programming into the object-oriented language of Java. The main function of lambda is to implement the transfer of function reference to obtain a more flexible implementation. Of course, many features of JAVA8 will be used in combination with lambda.

Java8 new features of Lambda

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.