Win a copy of Functional Design and Architecture this week in the Functional programming forum!
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
• Ron McLeod
• Rob Spoor
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Henry Wong
• Liutauras Vilda
• Jeanne Boyarsky
Saloon Keepers:
• Jesse Silverman
• Tim Holloway
• Stephan van Hulst
• Tim Moores
• Carey Brown
Bartenders:
• Al Hobbs
• Mikalai Zaikin
• Piet Souris

# Dan Chisholm's Question on java.lang.Math.abs

Ranch Hand
Posts: 190
• • Number of slices to send:
Optional 'thank-you' note:
• • The question is as follows:

The java.lang.Math.abs method can return which of the following?
a. Negative infinity
b. Positive infinity
c. NaN
d. Short.MIN_VALUE
e. Integer.MIN_VALUE
f. Long.MIN_VALUE
g. Negative zero
h. Positive zero

Positive infinity;
NaN;
Integer.MIN_VALUE;
Long.MIN_VALUE;
Positive zero

The explanation is:

The method name abs is overloaded. There are versions that accept one argument of type int, long, float or double. The type of the return value is the same as the argument type. The result is positive infinity if the argument is negative infinity. The result is positive zero if the argument is negative zero. The result is NaN if the argument is NaN. 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.

However, after reading the explanation, I still could not understand why Short.MIN_VALUE is not part of the answer while Integer.MIN_VALUE and Long.MIN_VALUE are.

Could anyone help?

Ranch Hand
Posts: 1865
• • Number of slices to send:
Optional 'thank-you' note:
• • Ken,

Thank you for using my exam.

The method name abs is overloaded. There are versions that accept one argument of type int, long, float or double. The type of the return value is the same as the argument type as long as the argument is one of the four types mentioned earlier: int, long, float or double. 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 surprisingly Integer.MIN_VALUE.

If the argument is Short.MIN_VALUE, then the type of the argument will be promoted to type int by method invocation conversion. More specifically, the argument of type short that has the value Short.MIN_VALUE will be promoted to type int and the value will be -32768. The absolute value of negative 32768 is positive 32768, and that positive value is well within the range of type int; so that's the value that will be returned by the abs method.

The abs method is able to correctly calculate the absolute value of Short.MIN_VALUE, because method invocation conversion promotes the type of the argument from type short to type int. The abs method does not correctly calculate the absolute value of Integer.MIN_VALUE, because the argument is not promoted to a type that has a range that is wide enough to accommodate the absolute value of Integer.MIN_VALUE.
[ May 26, 2007: Message edited by: Dan Chisholm ] Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?