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
The answer given is:
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?
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 ]
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Rocket Oven Kickstarter  from the trailboss
https://coderanch.com/t/695773/RocketOvenKickstartertrailboss
