Comparable Interface

Objects that have an ordering are compared using the compareTo() method.

5. Natural Order


five.compareTo( ten ) is negative.

The declaration of the two Integer variables looks wrong, but they are actually correct.
Autoboxing automatically constructs Integer objects for the two primitive integers.

Natural Order


Objects that implement compareTo() can be arranged into a natural order. This order can be visualized as an arrangement of objects from left to right as on a number line.

The strings in the picture are arranged left to right in dictionary order.

If an object A is left of another object B, then


is negative. For example, X.compareTo("orange") is negative for all the fruit X left of "orange".

Question 5:

What is "grape".compareTo( "banana" );