Hacker News new | ask | show | jobs
by ikusalic 4417 days ago
Here's one absurd bug in Java that really surprised me:

Math.abs(-2147483647); // 2147483647

Math.abs(-2147483648); // -2147483648

When you know this behavior, it's kinda obvious why, but still...

2 comments

Well, not just kind of obvious why, but kind of obvious it's not a bug in Java! If you're dealing with quantities that don't fit in a 32-bit twos complement integer, you need to use a larger datatype.
well, i'd only consider that a bug if java defines that integers will use at least as many bits as needed to store their value.

i'd guess this is not the case (but i don't know for sure)