Interface java.lang.Comparable

Interfaces are used frequently in the Java run-time library. Here is the source of interface Comparable in the java.lang package up to JDK 1.4. (We'll show the JDK 1.5 code, which is different, soon).

public interface Comparable {   public int compareTo(Object o);}

All classes that implement Comparable have to provide a compareTo() method. It allows two objects of the same class to be put in order with respect to one another, that is, they can be compared.

If you read the API documentation, you will see that implementations of compareTo() must return a negative, zero, or positive int, depending on whether “this” is smaller, equal to, or greater than the object parameter. There isn't any way for the compiler ...

Get Just Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.