aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Integer.MIN_VALUE" Watch "Doubt in Integer.MIN_VALUE" New topic
Author

Doubt in Integer.MIN_VALUE

Tejaswini Shirkhedkar
Greenhorn

Joined: Jun 22, 2004
Posts: 17
public class ADirtyOne
{
public static void main(String args[])
{
System.out.println(Math.abs(Integer.MIN_VALUE));
}
}


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
Greenhorn

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

Teju
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Integer.MIN_VALUE