Java Note (4): Lamda expressions

Source: Internet
Author: User

Reference Material Magic Music Technology Net Lesson: Http://www.mldn.cn/course/40/task/512/show

I. The role of LAMDA expression

Lamda is a function-based programming approach. Java is an object-oriented programming language, meaning that all operations are based on classes, and all functions are defined in the class. Many developers accustomed to functional programming feel that it is not good to use, in order to attract more developers, java1.8 finally launched the LAMDA expression.

But it is not the function programming that begins with LAMDA expression java. The first is implemented through anonymous inner classes (mainly interfaces).

Review anonymous internal classes

InterfaceMessage { Public voidprint (String str);} Public classMain { Public Static voidMain (string[] args) {Message msg=NewMessage () {@Override Public voidprint (String str) {System. out. println (str);        }        }; Msg.print ("Hello"); }}

Output results

Hello

Visible from the above code, the purpose is to output a statement, but because of the Java class structure constraints, so the code is too complex.

Using LAMDA expressions to implement

Interface Message {    publicvoid  print (String str);}  Public class Main {    publicstaticvoid  main (string[] args) {        = (s)- > System.  out . println (s);        Msg.print ("hello");}    }

First do not look at the syntax, at least through the LAMDA expression, this statement is less, without the Java class structure too restrictive.

Second, the basic grammar

Message msg = (s) of the above program, SYSTEM.OUT.PRINTLN (s); is a LAMDA expression.

The form is:

(parameter, the name can be arbitrarily up), the method body (multiple lines to add {}, only the return statement to omit the return)

The composition requirements of the LAMDA expression are as follows:

(parameter): the same as the parameter type of the print () method defined by the message interface, but no declaration is required here.

-A: Is a fixed syntax that indicates that a parameter is pointed to a method body.

Method Body: The method body code that was written when the anonymous inner class implementation method was first implemented.

There is one of the most important defining requirements when using the Lamda method: There can only be one method in the interface.

1. The most important purpose of LAMDA expression is to solve the problem of anonymous inner class

2, if you want to use the LAMDA expression, must be based on the interface, and the interface can have only one image method

Iii. Definition of function interfaces

It has been stressed that if you want to implement LAMDA expression, then there must be a premise, this premise is the interface within the definition of the extraction method can only exist one, so in order to strictly such a grammar requirements. You can use a functional interface definition.

To define a function interface:

@FunctionalInterface Interface Message {    publicvoid  print (String str);}

This can limit the interface in the abstract method only one, more error

Java Note (4): Lamda expressions

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.