• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Integer.MIN_VALUE

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be the result of attempting to complie & run the following code?
public class minval
{
public static void main(String args[])
{
System.out.println(Math.abs(Integer.MIN_VALUE));
}
}
(a)Causes a compilation error
(b)Causes no error and the value printed on the screen is less than zero.
(c)Causes no error and the value printed on the screen is one more than the Integer.MAX_VALUE
(d)Will throw a runtime exception due to overflow - Integer.MAX_VALUE is less in magnitude than Integer.MIN_VALUE.

The result is (b).
An absolute +ve value is displayed in the case of Byte.MIN_VALUE & Short.MIN_VALUE.Why not so in the case of an int.?How does it actually work ?

 
Greenhorn
Posts: 15
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that you get positive values from Byte_MIN and Short_MIN because there is no Math.abs function that takes a byte or a short as an argument. therefore when a byte or a short are passed in to the argument they are converted into an int.
the min value of a byte in 2's complement is: (binary) 1000000 = -128
so when this is converted to an int it is represented by:
0000 0000 0000 0000 0000 0000 1000 0000, and the one is no longer the highest bit (and is therefore not negative) and is therefore +128 instead of -128.
 
Savio Mascarenhas
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by peter waterhouse:
I believe that you get positive values from Byte_MIN and Short_MIN because there is no Math.abs function that takes a byte or a short as an argument. therefore when a byte or a short are passed in to the argument they are converted into an int.
the min value of a byte in 2's complement is: (binary) 1000000 = -128
so when this is converted to an int it is represented by:
0000 0000 0000 0000 0000 0000 1000 0000, and the one is no longer the highest bit (and is therefore not negative) and is therefore +128 instead of -128.


Peter,
What happens to Integer.MIN_VALUE ?Why does the negative sign still persist even after abs() ?
 
peter waterhouse
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the Integer.MIN_VALUE is an absolute value in itself therefore it returns itself when you apply abs to it.
note that the same thing happens when you apply Long.MIN_VALUE
From the Docs:
Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.
Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.
 
We noticed he had no friends. So we gave him this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic