• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Math.abs(Integer.MIN_VALUE)) not returning abs val

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI All
In the following code why the last line's output is so

int k=Integer.MAX_VALUE;
System.out.println("Integer.MIN_VALUE \t" + Integer.MIN_VALUE);
System.out.println("Math.abs( k )\t" + Math.abs(k));
System.out.println("Math.abs( Integer.MIN_VALUE )\t" + Math.abs(Integer.MIN_VALUE));
output is:
Integer.MIN_VALUE -2147483648
Math.abs( k ) 2147483647
Math.abs( Integer.MIN_VALUE ) -2147483648

Thanks
kundan
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look into the java api,
API
it says for the abs method of Math class:


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.


So, u get such output.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic