• 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

abhilash

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ADirtyOne
{
public static void main(String args[])
{
System.out.println(Math.abs(Integer.MIN_VALUE));
}
}
an attempt to compile and run the above class will

The op:Minimum value of int.(-2147..).
How is it evaluated.
Thanks!
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
min value of int is -2^32 .
we r giving this as a argument of abs function ..which removes
negative sign from it ..
got it ...
manal....
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But isn't the abs(Integer.MIN_VALUE) greater than maximum for Integer?
 
manal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got ur point ..
the arguement may be it should work without casting bcos we r printing it. println method is overloaded nd it can take long values as well..
BUT the internal structure(most general) of abs()method should be like this
static int abs(int i) {
if (i < 0) { i = -i;}
return i;
}
the original method dosent throw any exception . they have handled it inside the method ...they hava also made a provision so that return type should always be an int.
manal...
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is a special case in the implementation of the abs()
method of Java 2 API. Check-out the Math class, methods and the abs
method which takes the int argument. I am pasting it here ....
abs
public static int abs(int a)
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.
Parameters:
a - an int value.
Returns:
the absolute value of the argument.
See Also:
Integer.MIN_VALUE

One reason why I like Java...every little detail seems to be
taken care of...don't you think?
regds.
- satya
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic