This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Integer.MIN_VALUE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Integer.MIN_VALUE" Watch "Doubt in Integer.MIN_VALUE" New topic

Doubt in Integer.MIN_VALUE

Tejaswini Shirkhedkar

Joined: Jun 22, 2004
Posts: 17
public class ADirtyOne
public static void main(String args[])

Why does this class return a negative value and not the negation of an argument i.e. a positive value?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The reason for this is due to the way negative values are stored in Java. Because Java uses 2's complement to store negative numbers, the range of any integral value is always one value greater on the negative side than on the positive side. For example, the range of a byte is -128 to 127. You can't fit the absolute value of Byte.MIN_VALUE, which would be +128, into a byte. The same holds true for an int. The range of an int is -2^31 to 2^31 - 1.

SCJP Tipline, etc.
Tejaswini Shirkhedkar

Joined: Jun 22, 2004
Posts: 17
It cleared my concept, really.

I agree. Here's the link:
subject: Doubt in Integer.MIN_VALUE
Similar Threads
Simple code strange output aleast for me
Value of Integer.MIN_VALUE & -Integer.MIN_VALUE