DEV Community


Discussion on: Math.min returns Infinity?

jakebman profile image

Here's another one to blow your mind:

In Java, Math.abs(Integer.MIN_VALUE) is negative

This is per-spec:

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.

Because |Integer.MIN_VALUE| > Integer.MAX_VALUE, there's no way to represent it as a positive value in only 32 bits. It's basically overflow. C# throws an OverflowException, for instance.
Java doesn't throw on overflow, so it handles it this way.

dance2die profile image
Sung M. Kim Author

You are blowing my mind 🤯💥