wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Comparable interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparable interface?" Watch "Comparable interface?" New topic
Author

Comparable interface?

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

You would expect that the sort method in the Arrays class is defined to accept a
Comparable[] array so that the compiler can complain if anyone ever calls sort with an array
whose element type doesn’t implement the Comparable interface. Sadly, that is not the case.
Instead, the sort method accepts an Object[] array and uses a clumsy cast:
// from the standard library--not recommended
if (((Comparable) a[i]).compareTo(a[j]) > 0)
{
// rearrange a[i] and a[j]
. . .
}

P.244 - Core Java Fundamentals 8th Edition

Would you kindly clarify with an application? Why would I want to a Comparable[] interface?


Jon
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2053
1. As per current implementation:

pass a non-Comparable (doesnt implement Comparable)
> no compile error
> at runtime, , it will throw the class cast exception.

2. If done using Comparable[]

pass a non-Comparable (doesnt implement Comparable)
> compile error

===========

As seen above, it is clearer, more type-safe and less error-prone, to use Comparable[].
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Jesus Angeles wrote:1. As per current implementation:

pass a non-Comparable (doesnt implement Comparable)
> no compile error
> at runtime, , it will throw the class cast exception.

2. If done using Comparable[]

pass a non-Comparable (doesnt implement Comparable)
> compile error

===========

As seen above, it is clearer, more type-safe and less error-prone, to use Comparable[].


That is to say that when trying to Compare() classes which do not implement Comparable interface, you either get a (checked) exception or a compile error.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2053
Jon Camilleri wrote:
That is to say that when trying to Compare() classes which do not implement Comparable interface, you either get a (checked) exception or a compile error.


Yes, an exception (during runtime, as per current api), or a compile error (if the api is what the author was suggesting - Arrays.sort(Comparable[]).

Just to add...

There is no such thing as Compare(). It is Arrays.sort().

1. Currently, the API is Arrays.sort(Object[]).

2. The author is suggesting that it would have been better if it was: Arrays.sort(Comparable[])

Example:

java.awt.Button does not implement Comparable. If you pass an array of Button to the .sort(), meaning Arrays.sort(Button[]), you get below:

1. Currently, the API is Arrays.sort(Object[]).

> no compile error
> at runtime, since you passed Button[], it will try to cast Button, into a Comparable. Thereby throwing class cast exception.

2. The author is suggesting that it would have been better if it was: Arrays.sort(Comparable[])

> since you are passing Button[] where it is required to pass Comparable[], a compile error will happen. The error will be something like "The method sort(Comparable[]) in the type Arrays is not applicable for the arguments (Button[])".

=====================================



I suggest that you study the Comparable interface.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

You know, Jon, you have found an inconsistency in the API.

java.util.Collections has two methods for sorting Lists:
public static <T extends Comparable<? super T>> void sort(List<T> list)
public static <T> void sort(List<T> list, Comparator<? super T> c)

Based on this I would actually expect the following four methods in java.util.Arrays:
public static <T extends Comparable<? super T>> void sort(T[] a)
public static <T extends Comparable<? super T>> void sort(T[] a, int fromIndex, int toIndex)
public static <T> void sort(T[] a, Comparator<? super T> c)
public static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)

However, the first two don't use a method generic type but plain old Object[] instead. This has not been fixed for Java 7 (yet).

There must be a very good reason why Sun didn't do this, though. I looked in their (well, Oracle's) bug database but I couldn't find any entry about this. That means that nobody got bothered by this before.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparable interface?