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

Answer:

positive


Rules for compareTo()

With all objects, compareTo() works the way number comparisons work in ordinary arithmetic. Here are a few rules. Most of these are sensible if you think about numbers. Say that A, B, and C are Integers.


If A.compareTo(B) > 0 then B.compareTo(A) < 0.

Number Line, with B left of A

 

If A.compareTo(B) > 0 and B.compareTo(C) > 0 then A.compareTo(C) > 0.

Number Line, with C left of B, and B left of A

 

If A.compareTo(B) == 0 then A.compareTo(Z) and B.compareTo(Z) gives the same result, no matter what Z is.

Number Line, with B and A on same point

The classes that come with Java follow these rules. If you write a class that implements Comparable, you need to follow these rules. This is not hard to do because most sensible compareTo() methods will do this naturally.


QUESTION 6:

Say that X.compareTo(Y)==0.

Is it then true that X.equals(Y)?


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