aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help with an array sort problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help with an array sort problem" Watch "help with an array sort problem" New topic
Author

help with an array sort problem

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Given the code. What is the result?



A) "2 1" is printed.
B) "1 2" is printed.
C) Compilation fails.
D) An exception is thrown at runtime.


WHAT IS THE ANSWER, AND WHY?

My answer is D, because when you execute the line Arrays.sort(arr);, strings and HashTest objects are not comparable, so an exception would be thrown.

The given answer to this is D, but with this explanation: A ClassCastException is thrown: HashTest cannot be cast to java.lang.Comparable
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1469
    
    5

Hi Rachel,

If you check the documentation of Arrays.sort, it says that when an array of Object is passed to Arrays.sort, all the members of the array must implement Comparable interface.

Technically, it doesn't matter if actual objects are of different class as long as all those classes implement Comparable (or, to be specific, Comparable<Object>).

In your code, HashTest class does not implement Comparable interface, and hence the ClassCastException.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Anayonkar Shivalkar wrote:Hi Rachel,

If you check the documentation of Arrays.sort, it says that when an array of Object is passed to Arrays.sort, all the members of the array must implement Comparable interface.

Technically, it doesn't matter if actual objects are of different class as long as all those classes implement Comparable (or, to be specific, Comparable<Object>).

In your code, HashTest class does not implement Comparable interface, and hence the ClassCastException.

I hope this helps.


What is causing the actual exception to be thrown to be ClassCastException? Is is trying to convert a HashTest to a String, or vice versa?
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Also, EVEN IF HashTest DID implement the Comparable interface and overrode the compareTo() function, would that need to be written to compare to a string?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1469
    
    5

Rachel Glenn wrote: What is causing the actual exception to be thrown to be ClassCastException? Is is trying to convert a HashTest to a String, or vice versa?

Actual ClassCastException, as mentioned in your previous error message, is while casting HashTest to Comparable.
Rachel Glenn wrote: Also, EVEN IF HashTest DID implement the Comparable interface and overrode the compareTo() function, would that need to be written to compare to a string?

Now, that's tricky. If your HashTest implements Comparable<Object>, then you must assume that other object (with which current object is being compared to) must be of type Object (which is no big deal), AND, only methods of Object class can be invoked on it.
Of course, you can cast it to HashTest and all, but be prepared for another ClassCastException.

Rule of thumb here is (ok, not exactly a thumb rule):
If you want to sort an array with objects of different classes (e.g. class X and Y), then make sure that:
1) X and Y have common parent (say Z)
2) X and Y implements Comparable<Z>
3) Z holds methods which will matter in compareTo methods of X and Y
4) Create an array of Z and put those objects of X and Y in it

This way, you won't find much trouble while sorting.

Logically, it doesn't make much sense to compare HashTest with String (ever heard of comparing apples with oranges? )

I hope this helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with an array sort problem
 
Similar Threads
Generics and collections Again
HashSet Size
here is a question and i have a very similar code but i need to convert it
How is this printing 3, i have overridden the hashcode and equals method in here
Please explain the output