The only int value for which it works is Integer.MIN_VALUE.

It's because integers are negated using the two's complement way.



you see that Integer.MIN_VALUE is


Taking the negative value is done by first swapping 0 and 1, which gives


and by adding 1, which gives


As you can see in the link I gave, Wikipedia mentions the problem with the most negative numbers and specifies it's the sole exception :

The most negative number in two's complement is sometimes called "the weird number," because it is the only exception.

Of course you have the same phenomenon for Long.Min_Value if you store it in a long variable.

Note that this is only due to choices that were made regarding the binary storage of ints in Java. Another (bad) solution could for example have been to negate by simply changing the most significant bit and letting the other bits unchanged, this would have avoided this problem with MIN_VALUE but would have made 2 different 0 values and complicated binary arithmetic (how would you have incremented for example ?).