I have a class, which I have simplified to this:
final class Thing {
private final int value;
public Thing(int value) {
this.value = value;
Integer overflow… or more precisely, underflow.
Instead, do an explicit comparison:
private static final Comparator reverse = new Comparator() {
public int compare(Thing a, Thing b) {
int av = a.getValue(), bv = b.getValue();
return (av == bv) ? 0 : ((av < bv) ? -1 : +1);
}
};
Using subtraction is fine if you are sure that the difference won't "wrap around". For example, when the values in question are constrained to be non-negative.