wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer.MIN_VALUE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer.MIN_VALUE" Watch "Integer.MIN_VALUE" New topic
Author

Integer.MIN_VALUE

John Wolf
Ranch Hand

Joined: Feb 18, 2002
Posts: 64
Could someone please explain the following: -

What happens when you try to compile and run the following class...


The answer is that it would print two same -ive numbers. I thought it might throw an OverFlowException since int range is -2147483648 to 2147483687.

TIA!
Sumit


SCJP 1.2, SCJP 1.4, SCJA, SCWCD 1.4<br /> <br />------------------------------<br />
Ariel Ortiz
Ranch Hand

Joined: May 14, 2004
Posts: 121
There is no OverflowException in the Java API. Java integer arithmetic does not produce overflows, I just quietly wraps around.

In your example, Integer.MIN_VALUE == -(2^31). In binary that value is

1000 0000 0000 0000 0000 0000 0000 0000

When a value is the operand of the unary minus operator, you just need to compute it�s two�s complement: change all ones to zeros, all zeros to ones, add one to that result, and ignore the carry that goes out of the 32nd bit. If you do this with the above value, you get the same value you started with.

...Ariel
John Wolf
Ranch Hand

Joined: Feb 18, 2002
Posts: 64
Thanks Ariel! I get it now.

For all integer values x, -x equals (~x)+1.

Cheers!~
Sumit
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Related: http://qa.jtiger.org/GetQAndA.action?qids=15


Tony Morris
Java Q&A (FAQ, Trivia)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Integer.MIN_VALUE
 
Similar Threads
Integer.MIN_VALUE
Math.abs()
Can anyone pls explain about the output of this prog.
Simple code strange output aleast for me
~ operator