This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Dan Chisholm's Question on java.lang.Math.abs  RSS feed

 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



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?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!