A
Andreas Leitgeb
Suppose I've got a class that contains an "int" field.
Then I want to make my class "Comparable", and the
order depends on the int-field.
what's the standard idiom to use in the overridden
compareTo method?
return that.intVar - this.intVar;
is surely wrong, due to wraparound, as one can see when
comparing MAX_VALUE to MIN_VALUE
Using Integer.compareTo() involves boxing twice, which feels
too heavy.
Does one need to nest two ?:-operations (or if's), or is
there some nicer idiom?
Then I want to make my class "Comparable", and the
order depends on the int-field.
what's the standard idiom to use in the overridden
compareTo method?
return that.intVar - this.intVar;
is surely wrong, due to wraparound, as one can see when
comparing MAX_VALUE to MIN_VALUE
Using Integer.compareTo() involves boxing twice, which feels
too heavy.
Does one need to nest two ?:-operations (or if's), or is
there some nicer idiom?