In a compareTo(), there are 3 possibilities. The Object passed into compareTO() is either less then, equal to, or greater then the value of the called object. A returned value < 0 is considered to be less then, greater then is >0 and equals is ==0. This is convention and should be used unless you want to annoy people, but is not enforced by the compiler or JVM.
Returning the difference could be used, but makes things more complicated, and would only be suitable in specific cases. If you are comparing Strings, how would you calculate a difference, and what use would it be. Between integral values, it would work, but if there isn't a need to know how much they are off, then what is the point of the extra calculations?
You also need to declare that your class implements the Comparable interface.
Here is a simple Comparable class:
Using the class
Of course this means you can reference MyInt objects as Comparable references. Which can be useful sometimes.
If you are using
Java 1.5 or higher you can use generics to make things simpler:
[ February 24, 2007: Message edited by: David McCombs ]
[ February 24, 2007: Message edited by: David McCombs ]