In additional to the answers given so far...
There is four values in total
int i = Integer.MIN_VALUE;
long i = Long.MIN_VALUE;
Integer i = Integer.valueOf(Integer.MIN_VALUE);
Long i = Long.valueOf(Long.MIN_VALUE);
The wrapped values get unwrapped so they are also true for this expression.
Note: Math.abs documents.
public static int abs(int a)
Returns the absolute value of an int
value. If the argument is not negative, the argument is returned. If
the argument is negative, the negation of the argument is returned.
Note that if the argument is equal to the value of Integer.MIN_VALUE,
the most negative representable int value, the result is that same
value, which is negative.
and
public static long abs(long a)
Returns the absolute value of a long value. If the argument is not
negative, the argument is returned. If the argument is negative, the
negation of the argument is returned.
Note that if the argument is equal to the value of Long.MIN_VALUE, the
most negative representable long value, the result is that same value,
which is negative.
It is surprising that Math.abs might return a negative number. This happens either because a) there is no positive values for -MIN_VALUE in these cases b) performing the -
calculation results in an overflow.
What is also interest is why doesn't Byte.MIN_VALUE, Short.MIN_VALUE do not do this. This is because the -
changes the type to int
for these and thus no overflow.
Character.MIN_VALUE doesn't have a problem because it is 0.
Float.MIN_VALUE and Double.MIN_VALUE have a different meaning. These are the smallest representable value greater than zero. Thus they have valid negative values which are not themselves.