The only `int`

value for which it works is `Integer.MIN_VALUE`

.

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

Using

`System.out.println(Integer.toBinaryString(Integer.MIN_VALUE));`

you see that `Integer.MIN_VALUE`

is

`10000000000000000000000000000000`

Taking the negative value is done by first swapping `0`

and `1`

, which gives

`01111111111111111111111111111111`

and by adding `1`

, which gives

`10000000000000000000000000000000`

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 ?).