According to the documentation the method returns an int greater than 0 if it is greater or less than 0 if it is less, but looking at the method you see that it always returns -1, 0, 1.
Returns: the value 0 if x == y; a value less than 0 if x < y; anda value greater than 0 if x > y
And then the code:
/**
* Compares two {@code int} values numerically.
* The value returned is identical to what would be returned by:
* <pre>
* Integer.valueOf(x).compareTo(Integer.valueOf(y))
* </pre>
*
* @param x the first {@code int} to compare
* @param y the second {@code int} to compare
* @return the value {@code 0} if {@code x == y};
* a value less than {@code 0} if {@code x < y}; and
* a value greater than {@code 0} if {@code x > y}
* @since 1.7
*/
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
So my question is, when you implement a comparable, do you have to check that you get -1,0,1 or Integers greater than, equal to or less than 0?
It is clearly explained, in the comments of the JDK 8 source code , which is the case you expose:
It may not be very clear in the documentation of the class , but it is understood:
Translated and dissected, to make it clear, it means:
That is,
compare()
it returns only three possible values:0
Yesx == y
-1
Yesx < y
1
Yesx > y
Proof
Using the JDK 8 source code method above:
Departure:
To add to the comments and answers so far:
compareTo
The class methodInteger
itself isn't very useful if you call it like this:Since it could have been done directly like this:
The method
compareTo
makes sense if you use it to compare more complex structures like a String or other types of objects.That said, why would we want to know if one object is "greater" or "lesser" than others? The most used case is to define an order when you have a collection of said objects. In this way we could order the elements from largest to smallest or vice versa.
In these cases it is when we define our compare function, since
Integer
and String provide their own implementation of compareTo, but for the classes that we create we have to define it.Imagine we have a Car class, with make and model attributes.
In this class, we have implemented the interface
Comparable<Coche>
to be able to compare two cars with each other. For this implementation, what is done is to compare the mark first (using thecompareTo
class methodString
). In the event that they are equal, that is, a 0 is received, it is compared once more; but this time the model of the car is compared.Once this is done, we can already know if a car is "greater" or "lesser" than another in the sense of whether it has to be ordered "before" or "after" in a collection. In this way "Seat León" is greater than "Seat Panda" and this in turn greater than "Toyota Yaris"
We can implement sorting like this:
The output is:
What I want you to see with this example is that we have not explicitly called our function
compareTo
from the main program . Rather, we have calledCollections.sort
, which internally has called our methodcompareTo
, to determine which car is larger or smaller than another and thus be able to sort the list.On the other hand, from our method
compareTo
there is no problem in calling other methodscompareTo
of other classes, as we have done to compare the character strings that are our make and model of car.