• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Integer.MIN_VALUE

 
Tejaswini Shirkhedkar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tejaswini Shirkhedkar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
It cleared my concept, really.

Teju
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic