In Java 8 there is "Method Reference" feature. One of its kind is "Reference to an instance method of an arbitrary object of a particular type"
http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html#type
Can someone explain what does "arbitrary object of particular type" mean in that context ?
An arbitrary object has those properties common to the individual objects in its range. So an arbitrary number is odd or even, an arbitrary man is mortal, since each individual number is odd or even, each individual man is mortal.
The Arbitrary Text Type of semantic type is one of the Text Format Types. This type consists of an arbitrary text string of any length provided by the user. The merge tool substitutes this arbitrary string into the templates specified in the Value column of the ModuleSubstitution table.
What Does Java Object Mean? A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state.
Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
The example given from the Oracle Doc linked is:
String[] stringArray = { "Barbara", "James", "Mary", "John", "Patricia", "Robert", "Michael", "Linda" }; Arrays.sort(stringArray, String::compareToIgnoreCase);
The lambda equivalent of
String::compareToIgnoreCase
would be
(String a, String b) -> a.compareToIgnoreCase(b)
The Arrays.sort()
method is looking for a comparator as its second argument (in this example). Passing String::compareToIgnoreCase
creates a comparator with a.compareToIgnoreCase(b)
as the compare method's body. You then ask well what's a
and b
. The first argument for the compare method becomes a
and the second b
. Those are the arbitrary objects, of the type String (the particular type).
Don't understand?
Read more at the source: http://moandjiezana.com/blog/2014/understanding-method-references/
It is a reference to an instance method from some type. In the case of the example, compareToIgnoreCase
is a method from String
. The program knows that it can invoke this method on an instance of String
, so it can take the reference and any object of that type and be guaranteed the method exists.
I would compare this to the Method
class in that they refer to a method and can be invoked on an arbitrary instance of some type.
For the example, it can use two String
objects and call compareToIgnoreCase
on one and use the other as an argument to match the method signature. This allows it to take the array and sort it based on any method of the array type instead of requiring a comparator instance to do this instead.
And here is the example for anyone who didn't click on the link in the question:
String[] stringArray = { "Barbara", "James", "Mary", "John", "Patricia", "Robert", "Michael", "Linda", "George" }; Arrays.sort(stringArray, String::compareToIgnoreCase);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With