JAVA8 Method References-java In a nutshell, 6th

Source: Internet
Author: User



The relationship between the JAVA8 function reference and the lambda expression => function Reference is a simplified lambda expression that infers only the function, parameter, and return value compiler that appears.

> Actually this grammar and  lambda expression is exactly the opposite,  LAMBDA expression represents an anonymous method, which is the absence of a function name, giving only parameters and method bodies. Then the existing function comes in handy, the existing function has the method name, gives the method name, the parameters and the method body have already, by the compiler inferred.

Note: Methods in Java cannot exist independently and must be included in the type, which is the limit @FunctionalInterface the lambda expression.

Recall that we can think the lambda expressions as methods that don ' t has names.

Now, the consider this lambda expression:

In real code this would probably is shorter because of type inference

(MyObject myObj), myobj.tostring ()

This is being autoconverted to an implementation of a @FunctionalInterface that

Have a single nondefault method, takes a single MyObject and returns String.

However, this seems like excessive boilerplate, and so Java 8 provides a syntax for

Making this easier to read and write:

Myobject::tostring

This is a shorthand, known as a method reference, which uses an existing method as a

Lambda expression. It can be thought of as using a existing method, but ignoring

The name of the method, so it is can used as a lambda, and autoconverted in the

Usual.



JAVA8 Method References-java In a nutshell, 6th

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.