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,
String::valueOf
Integer::compare
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 Integer.compare
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.
In the change log 0.63 of JSR-335 the term function descriptor has been changed with function type.
Might be the case for change that in your post.
Thanks.
Thanks, I’ve done that.
[…] code snippets and ideas taken from Maurice’s blog post […]
[…] method references and are handles for static methods in the Class. Just like lambdas, it is an alternative way of representing a […]