[JAVA] The return specification of compareTo can afford without going to the reference

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 compareToDescription 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

The return specification of compareTo can afford without going to the reference
[Rails] Button to return to the top of the page
I want to return the scroll position of UITableView!
How to add elements without specifying the length of the array
How to change the contents of the jar file without decompressing
Be sure to compare the result of Java compareTo with 0
9 Corresponds to the return value
[Rails] Talk about paying attention to the return value of where
[Rails] Read the RSS of the site and return the contents to the front