Java Platform 1.2
Beta 4

Interface java.lang.Comparable

All Known Implementing Classes:
Byte, Character, Double, File, Float, Long, ObjectStreamField, Short, String, Integer, BigInteger, BigDecimal, URL, Date, CollationKey

public abstract interface Comparable
This interface imposes a partial ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.

Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in SortedMaps or elements in SortedSets, without the need to specify a Comparator.

A class's natural ordering is said to be total if and only if e1.compareTo(e2)==0 implies that e1.equals(e2) for every e1 and e2 of class C. A partial ordering that is not total is said to be strictly partial.

Virtually all Java core classes that implement Comparable have natural orderings that are total. One Java core class whose natural ordering is strictly partial is java.math.BigDecimal. This is mentioned here because some APIs that call for Comparable objects (such as SortedSet and SortedMap) will behave differently depending on whether the objects' natural ordering is total or strictly partial.

Since:
JDK1.2
See Also:
Comparator, Collections.sort(java.util.List), Arrays.sort(Object[]), SortedSet, SortedMap, TreeSet, TreeMap

Method Summary
 int compareTo(Object o)
          Compares this Object with the specified Object for order.
 

Method Detail

compareTo

public int compareTo(Object o)
Compares this Object with the specified Object for order. Returns a negative integer, zero, or a positive integer as this Object is less than, equal to, or greater than the given Object.

The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.

The implementer must also ensure that x.equals(y) implies that x.compareTo(y)==0. Note that the converse is not necessarily true (e.g., BigDecimal).

Finally, the implementer must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.

Parameters:
o - the Object to be compared.
Returns:
a negative integer, zero, or a positive integer as this Object is less than, equal to, or greater than the given Object.
Throws:
ClassCastException - the specified Object's type prevents it from being compared to this Object.

Java Platform 1.2
Beta 4

Submit a bug or feature
Submit comments/suggestions about new javadoc look
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.
This documentation was generated with a post-Beta4 version of Javadoc.