Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "an Arbitrary Object of a Particular Type" mean in java 8?

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 ?

like image 847
Roman Ivanov Avatar asked May 08 '14 05:05

Roman Ivanov


People also ask

What are arbitrary objects?

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.

What is an arbitrary type?

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 object [] mean Java?

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.

What is an object in Java class 8?

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.


2 Answers

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?

  • Make sure you know what a comparator is and how to implement it.
  • Know what a functional interface is and how it affects lambdas in Java.
  • A comparator is a functional interface that's why the method reference becomes the body of the compare method inside the comparator object.
  • Read the source below for another example at the bottom of the page.

Read more at the source: http://moandjiezana.com/blog/2014/understanding-method-references/

like image 99
Jawad Avatar answered Sep 20 '22 14:09

Jawad


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); 
like image 31
Andrew Vitkus Avatar answered Sep 22 '22 14:09

Andrew Vitkus