In Java without operator overload, the size comparison of objects is the .compareTo
method, but can you tell which one returns positive, 0, or negative? I can't say
And if you find a specification of what the return value is positive, 0, or negative, can you understand the conditional expression of the if statement at a glance while referring to it, or can you write it down smoothly and definitely? ⋯ It's so easy!
Let's make a table
If there is an inequality sign | compareTo Description by |
---|---|
a < b |
a.compareTo(b) < 0 |
a <= b |
a.compareTo(b) <= 0 |
a > b |
a.compareTo(b) > 0 |
a >= b |
a.compareTo(b) >= 0 |
Ah.
Putting a 0 to the ** right side of compareTo
saves the inequality sign direction **.
As a result, the return value specifications were set so that it would be the case.
Recommended Posts