Java 8 Methods Reference (method references)

Source: Internet
Author: User
Tags instance method

1 What is a method reference (methods references)

Java 8 adds a familiar but unfamiliar symbol::. You might see this code.

System.out::p rintln

is actually a method reference (methods references). Since Java 8 also takes the method/function as the first input parameter. So you will see Inventory.comparing (apple::getweight); This "strange" code.


2 How to use method references (methods references)

In fact, a method reference is a syntactic sugar of a lambda expression. It is possible to simplify the code by means of a method reference when the lambda refers to only one method references.

(Apple a), a.getweight () = = = Apple::getweight (), Thread.CurrentThread (). DumpStack () = = = Thread.CurrentThread ( )::d umpstack (str, i), str.substring (i) = = = String::substring (String s), System.out.println (s) = = = System.out:: println


3 Types of method references


3.1

static method Reference

Example: Integer::p arseint (Parsetint is a static method)


3.2

Instance method

Example: String::length (Length () is an instance method)


3.3

Instance Object method Reference

Example:

Person p = new person

P::getname (P is an instance object)


This article is from the "Development" blog, make sure to keep this source http://jamesdev.blog.51cto.com/2066624/1858767

Java 8 Methods Reference (method references)

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.