4

Currently am working on an assignment that puts the concepts of Comparable to application.

I wrote this simple method that allows the input of a Comparable[] array. This method returns the minimum value of any given array input. Using the .compareTo() method, I am able to really extend this method to allow object types of really, anything, whether it's custom implementation like a Point class (which we did for the assignment, but not shown here).

public static Comparable getMinimum(Comparable[] inputArray)
{
    Comparable newObj = inputArray[0];
    for(int i = 0; i < inputArray.length; i++)
    {
        Integer retValue = (inputArray[i]).compareTo(newObj);
        if(retValue < 0)
        {
            newObj = inputArray[i];
        }
    }
    return newObj;
}

My question is - what is this Comparable type? Looking at the Java API, it didn't really reveal much. Why are we using the Comparable type as opposed to the Object type, which also includes types like int, double, String, etc?

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169

3 Answers3

5

A comparable type is a reference type whose class C implements the Comparable<C> interface. The interface defines one method: int compareTo(<T> other). Read the javadoc for details.

  • Primitive types (e.g. int and double) are not Comparable in that sense because primitive types are not reference types.

  • The type Object is a reference type, BUT it is not a comparable because it does not implement Comparable<Object>.

  • The type String is a reference type, AND it implements Comparable<String>. Therefore it is a comparable type. And this is clear from the javadoc for String which says:

    public final class String extends Object
        implements Serializable, Comparable<String>, CharSequence
    

The difference between primitive types and reference types is fundamental in Java. Primitive types are boolean, byte, char, short, int, long, float and double. Reference types are Java classes and Java array types.


Now in your example, you are actually using the Comparable interface as a raw type. If you compile this code with a Java 5 or later compiler, it will give you compiler warnings about using raw types. This doesn't substantially change the answer, but in effect you are now effectively talking about implementing Comparable<?> rather Comparable<T> and the method is effectively compareTo(Object).

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

You use Comparable because the type Comparable is generic, allowing you to specify exactly what type your are "comparable" to, while also keeping things type-safe. The Object base class does not contain such a thing.

Also, you're not using the generic version of Comparable. That's a problem, you're using the old type of Comparable.

AxiomaticNexus
  • 6,190
  • 3
  • 41
  • 61
  • 1
    Ah I see! So the difference between `Comparable` and `Object` is that it's type-safe? Also, where do you specify exactly what type you are comparable to? – theGreenCabbage Apr 12 '14 at 01:31
  • @theGreenCabbage That's the main reason. Also I noticed something in your code that I pointed out in my question edit. – AxiomaticNexus Apr 12 '14 at 01:33
  • It appears the professor's answer keys are also deprecated as well, since mine bares resemblance to his. What is the non-deprecated way to do this? – theGreenCabbage Apr 12 '14 at 01:34
  • @theGreenCabbage You use the generic version of `Comparable` instead: http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html – AxiomaticNexus Apr 12 '14 at 01:40
  • What is the difference between the generic version and the one you linked to, @YasmaniLlanes? – Mo2 Apr 12 '14 at 01:42
  • @Mo2 The one I linked to is the generic version. – AxiomaticNexus Apr 12 '14 at 01:42
  • I meant what is the difference between the one you linked to and the one OP is using, other than the diamond parameter? @YasmaniLlanes – Mo2 Apr 12 '14 at 01:44
  • 1
    @Mo2 That the generic one provides type safety. The class implementing `Comparable` won't have to cast the object passed onto `compareTo`. – AxiomaticNexus Apr 12 '14 at 01:48
0

Comparable is an Interface, not a class. Interface enforce any class which implements it to have the method implementations of the methods declared in the interface. Comparable interface declares a method called compareTo for comparing two objects (useful for sorting a set of objects). So if a class implements Comparable interface, we know for sure that we can compare the instances of that class. Object, doesn't declare any method for object comparison. So you cannot compare two objects directly.

suhe_arie
  • 352
  • 2
  • 7