I have a class, which I have simplified to this:
final class Thing {
private final int value;
public Thing(int value) {
this.value = value;
You cannot use minus to create the comparison. You'll overflow when the absolute difference exceeds Integer.MAX_VALUE
.
Instead, use this algorithm:
int compareInts( int x, int y ) {
if ( x < y ) return -1;
if ( x > y ) return 1;
return 0;
}
I like to have this function in a library for such purposes.