• 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.round()

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found this on a mock exam site.....
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
1. Cause a compiler error.
2. Cause no error and the value printed on the screen is less than zero.
3. Cause no error and the value printed on the screen is one more than Integer.MAX_VALUE
4. Will throw a runtime exception due to overflow - Integer.MAX_VALUE is less in magnitue than Integer.MIN_VALUE.
The answer given is surprisingly 2. I copiled and checked the answer and it is correct. What I do not understand is how is the Math.abs() method returning a -ve value.....
Any inputs???
Thanks,
Shafeeq
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I check api explain for the Math.abs() and find:
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.
Hope it will help you..
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maximum value that an integer can hold is 2147483647 and minimum is -2147483648 if you add one with maximum automatically sign bit will switch on.
Look at this code
class iPlay {
static public void main(String x[]) {
int x = 2147483648;
System.out.println(x);
}
}
Out put of the above code will be -2147483648
The same thing happens when you use Math.abs function
I hope you got the clue;
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

note that
int i = 2147483648;
will NOT compile, but
int i = 2147483647 + 1;
int i = 0x80000000;
int i = 020000000000;
all compile and print out i as -2147483648. So it seems that the compiler checks decimal literals to see if they are within range, but does not check hexadecimal or octal literals.
Can somebody explain this?
 
Look ma! I'm selling my stuff!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic