Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Java language puzzle...

+10 votes

Just for kicks, try this sometime:  

        System.out.println(Math.abs(-2147483648));

Does the result surprise you? 

If you can't figure out / understand why this happens, then you might want to take CSC 330 this winter.  (If you already took 330, and still can't figure it out, I can give you a hint...)

 

asked Sep 11, 2015 in other by Forrest Stonedahl (100 points)
P.S.  No, this isn't really related to Software Development.  I'm just posting it in the "other" category on the Q&A for fun.

1 Answer

+5 votes
Is it because of the storage? An Integer's max size is  2147483647. And at 2147483648 it "wraps around" so the abs on the int values does not work. Source: http://www.coderanch.com/t/231198/java-programmer-SCJP/certification/Math-abs-Integer-MIN-return (toward the bottom)
answered Sep 12, 2015 by Kelsey Self (100 points)
...