The Java analyzer has detected a narrowing cast of a subtraction in a comparison method that may flip the sign of the result.
Methods such as
Comparable) are used to check properties such as which of two values is greater or less than the other.
While subtracting two variables usually is a good way to compare two integral values, it is a bad idea to downcast the result to a smaller type.
Consider this case of two numbers being subtracted:
int a = 0x0000F09; int b = 0x0FFFFD30; int c = a - b; // 0xF00011D9 int d = (short)c; // 0x000011D9 !!!
d is completely different from
c because the 4 most significant bits of
F) have been removed due to the downcast from
short. This converts the value from a negative 2's complement value to a positive one.
Long.compare() to compare integers of any size instead. this method will return a correct, usable comparison result in all cases.
return Long.compare(a, b);