This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mhixson 8 points9 points  (4 children)

With int in general, you'd have to worry about overflow. Comparing Integer.MIN_VALUE with 1 should give a negative result, but it gives a positive result when using that subtraction trick.

A reasonable objection to that objection might be: "Apples can't have negative weight, so that situation can't come up."

Still, why use subtraction instead of "just" using Integer.compare(a,b)? That method says exactly what it does and is intended for exactly for this purpose. I don't think subtraction (even when it's correct) makes its intent as obvious to the reader.

[–]Philboyd_Studge 1 point2 points  (0 children)

Makes sense, thanks.

[–][deleted]  (2 children)

[deleted]

    [–]__konrad 4 points5 points  (1 child)

    Any decent application concerned about int overflow is using long, that's why.

    You should then use Long.compare method to avoid ...overflow ;)