Java 8 LAMBDA expression

Source: Internet
Author: User
Tags variable scope

The content comes from the Novice tutorial (invasion delete)

Lambda expressions, also known as closures, are the most important new features that drive Java 8 release.

LAMBDA allows the function to be used as a parameter of a method (the function is passed into the method as a parameter).

Using LAMBDA expressions can make your code more compact and concise.

Grammar

The syntax format for a lambda expression is as follows:

(parameters) , expression or (parameters) , { Statements; }

The following are important characteristics of lambda expressions:

    • Optional type declaration: you do not need to declare parameter types, and the compiler can recognize parameter values uniformly.
    • optional parameter parentheses: One parameter does not need to define parentheses, but multiple parameters need to define parentheses.
    • Optional curly braces: If the body contains a statement, you do not need to use curly braces.
    • Optional return Keyword: if the principal has only one expression return value, the compiler will return the value automatically, and the curly brace needs to specify that the expression returns a numeric value.
LAMBDA Expression Instances

Enter the following code in the Java8tester.java file:

Java8tester.java file
 Public classJava8tester { Public Static voidMain (String args[]) {Java8tester tester=NewJava8tester (); //type declarationmathoperation addition = (intAintb) A +b; //No type declarationMathoperation subtraction = (A, b) A-b; //return statements in curly bracesMathoperation multiplication = (intAintb), {returnAb;}; //no curly braces and return statementsMathoperation Division = (intAintb) A/b; System.out.println ("Ten + 5 =" + Tester.operate (10, 5, addition)); System.out.println ("10-5 =" + tester.operate (10, 5, subtraction)); System.out.println ("Ten x 5 =" + Tester.operate (10, 5, multiplication)); System.out.println ("10/5 =" + tester.operate (10, 5, Division)); //without parenthesesGreetingservice GreetService1 = message, System.out.println ("Hello" +message); //with parenthesesGreetingservice GreetService2 = (message), System.out.println ("Hello" +message); Greetservice1.saymessage ("Runoob"); Greetservice2.saymessage ("Google"); }     Interfacemathoperation {intOperation (intAintb); }     InterfaceGreetingservice {voidsaymessage (String message); }     Private intOperateintAintB, Mathoperation mathoperation) {         returnMathoperation.operation (A, b); } }            

Execute the above script and the output is:

$ javac Java8tester.java $ java java8tester+ 5 = 1510-5 = 510 x 5 = 5010/5 = 2Hello Runoobhello GOOGL E

There are two points to note when using LAMBDA expressions:

    • Lambda expressions are primarily used to define a method-type interface that executes within a row, for example, a simple method interface. In the example above, we use various types of lambda expressions to define the method of the Mathoperation interface. Then we define the execution of the saymessage.
    • LAMBDA expressions eliminate the hassle of using anonymous methods and give Java simple but powerful, functional programming capabilities.
Variable scope

A lambda expression can only refer to a final or final local variable, which means that local variables defined outside the domain cannot be modified inside the lambda, or the error will be compiled.

Enter the following code in the Java8tester.java file:

Java8tester.java file
 Public class java8tester {     finalstatic String salutation = "Hello!" ;       Public Static void Main (String args[]) {         = message, System.out.println (salutation + message);         Greetservice1.saymessage ("Runoob");     }      Interface Greetingservice {         void  saymessage (String message);}     }    

Execute the above script and the output is:

$ javac Java8tester.java $ java java8testerhello! Runoob

Java 8 LAMBDA expression

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.