Default Methods


Idioms and Techniques

Design Rationale

Advanced Questions

What are method references?

Any lambda expression may be thought of as an anonymous representation of a function type of a functional interface. An alternative way representing a function type is with a concrete method of an existing class. Method references are handles to such existing methods. For example,


are references to static methods, analogous to lambda expressions that do not capture any instance or local variables. (Instance method references are treated next.) For a trivial example, the method in the class java.util.Arrays

	public static <T> void sort(T[] a, Comparator<? super T> c);

expects a Comparator for its second argument. The method has a signature that is type-compatible with Comparator’s function type—that is, its compare method—so it would be legal to call Arrays.sort like this:

	Arrays.sort(myIntegerArray, Integer::compare)

In this simple example, the signature of the referenced method, Integer::compare, happens to match the (erased) function type of Comparator. In general, an exact match isn’t necessary: in such a call, the method reference can be seen as shorthand for a lambda expression made up from a formal parameter list copied from the function type and a body that calls the referenced method.

Notice that the syntax ReferenceType::Identifier used to reference static methods as in the examples above can be used to reference instance methods also.
Usually, the single method of this single-method interface. For a
more precise definition, see the second syntax note on this page.