go to previous page   go to home page   go to next page

Answer:

"apple", "orange", "plum".

This is the order that compareTo() would place the strings.


Comparable<T> Interface

An interface consists of constants and method declarations. The Comparable<T> interface consists of just one method (and no constants):

int compareTo( T obj )                        
Compare the object running the method with obj, which is of type T. Return a negative integer, zero, or a positive integer, when he object running the method is less than, equal, or greater than obj.

T stands for the type of the objects. If the objects are Strings, then T is String. Strings implement the Comparable<String> interface.

If an object is of a class that implements Comparable, then that object is less than, equal, or greater than any object of that class. compareTo() returns an integer to show which of these three relations hold.

  Relation   objectA.compareTo( objectB )
objectA Less Than objectB Negative Integer
objectA Equal objectB Zero
objectA Greater Than objectB Positive Integer

QUESTION 2:

Does compartTo() tell us enough?

If any two objects can be compared, and the result is less than, equal, or greater than is this enough to put a collection of objects in order?


go to previous page   go to home page   go to next page