• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh


Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in one of the dan explaination to question...there is written that

If the argument is a negative integral value, then the returned value is the two's complement of the argument. The magnitude of Integer.MIN_VALUE is one greater than the magnitude of Integer.MAX_VALUE; therefore, the absolute value of Integer.MIN_VALUE exceeds the range of Integer.MAX_VALUE. Due to the limited range of type int, the two's complement of Integer.MIN_VALUE is Integer.MIN_VALUE. For that reason, the Math.abs method returns Integer.MIN_VALUE when the argument is Integer.MIN_VALUE. The negation of Short.MIN_VALUE is well within the range of type int; so Short.MIN_VALUE is never returned by the Math.abs method.

i m not able to understand fully what does that means...
if anybody pls explain me with eg. would be gr8 help to me...
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The range of an int is -2^31 to (2^31)-1, which is -2147483648 to 2147483647. Note that the magnitude of the negative bound is one greater than that of the positive bound.

So the absolute value of Integer.MIN_VALUE is actually greater than Integer.MAX_VALUE. In particular, we might expect the absolute value to be +2147483648, but this is one greater than what can be expressed as a positive int, so this overflows to Integer.MIN_VALUE.

In terms of twos complement, flipping the bits of Integer.MIN_VALUE results in Integer.MAX_VALUE, then adding 1 causes the overflow. Note the output from the code below...

This is not a concern when a short is passed to the Math.abs(int) method, because the short is widened to type int, which can easily accomodate Short.MAX_VALUE + 1.
[ May 12, 2005: Message edited by: marc weber ]
I am mighty! And this is a mighty small ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic