Lambda Expressions Explanation of Java 8

Source: Internet
Author: User

Lambda Expressions Explanation of Java 8
Java 8 Adds the purpose of Lambda Expressions: to make the code simple and easy to read and eliminate excessive redundant code (as much as possible to make the compiler do code conversion work, it can also be considered as the role of syntactic sugar). people who have used the C and C + + languages remember that their function arguments can be function pointers as code snippets (some behaviors), whereas in the Java language, to make a function a parameter, you must encapsulate the function as an object and pass the argument to the object. the new Lambda Expressions in Java 8 makes this situation mitigated (concepts such as functional change and closures are transitive, not objects).
Take a look at the code example:
Package Com.doctor.lambda;import Java.util.function.binaryoperator;import java.util.function.consumer;/** * @author        Sdcuike * * Created on June 7, 2016 morning 12:03:18 */public class Demo1 {public static void main (string[] args) {        Runnable Runnable = (), System.out.println ("Test lambda");        New Thread (runnable). Start ();        String msg = "Hello";            runnable = (), {System.out.println (msg);            System.out.println ("Run 1");        System.out.println ("Run 2");        };        New Thread (runnable). Start ();        consumer<string> Consumer = message, SYSTEM.OUT.PRINTLN (message);        Consumer.accept ("Hello Java lambda");        binaryoperator<long> add = (A, b), A + B;        System.out.println (Add.apply (66L, 22L));        Add = (long A, long B), A + B;    System.out.println (Add.apply (66L, 33L)); }}


previously, the parameters of methods in Java were mostly special anonymous class objects (there was only one abstract method in the interface- Functional Interfaces
-A functional interface is an interface with A single abstract method which is used as the type of A Lambda expres Sion.
). The Java LAMBDA simplifies the syntax without having to display the new anonymous class object, but instead (parameter), {method Body}, if the parameter is only one parameter, you can omit {} on both sides of the parameter, similarly, the method body has only one statement, or you can omit {}. The type of the parameter can also be omitted and deduced by the compiler (the actualderivationnot out, we write the parameter type to the compiler to prompt how to handle- Type Inference), or remember the words: can save the province.
if the method body uses a variable outside the method body, the variable compiler thinks it isfinal, this is the same as the rule we used to use anonymous classes, but the JAVA8 syntax relaxes the limit, and the variables don't have to befinal, but must be a valid final variable.

Lambda Expressions Explanation of Java 8

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.