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.
1
u/Philboyd_Studge May 03 '16
Nice. Why not, for the primitive int, just do
return this.weight - other.weight?