This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

Is there any specific reason why the MIN_VALUE of Float & Double class are constants holding the smallest positive nonzero value of type float/double and not the negative number. I would always expect that MIN_VALUE of a class means you cannot have a object having a value less than MIN_VALUE. But was amazed to see the output of the following Code.

o/p fl1 = -1.23454 fl2 = 1.4E-45 fl is less than MIN_VALUE fll is less than MIN_VALUE

It is due to the way in which floating point numbers are stored. Unlike integers, floats and doubles are stored in binary as an exponent and a mantissa (a Google search will show how these work). Simplified a bit, if you think of the number 2x10^3 (==2000), the 2 is the mantissa and the 3 is the exponent. Both the exponent and the mantissa have a sign bit. For MAX_VALUE neither will be set, and the value is the largest that can be stored given the width of the type. The most negative number that can be stored in a Float or Double is simply equal to MAX_VALUE with the mantissa sign bit set, or in other words "-MAX_VALUE". As this is easy to work out there is not much point in storing it. It is more useful to know the smallest number that can be stored - this will have the exponent sign bit set (so it will be very small - given the example above, 2x10^-3 == 0.002) but not the mantissa sign bit. That is what is stored in MIN_VALUE. Hope that is clear - or at least clearer Ben.

SCJP1.4, SCJD, SCEA (in progress)

shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109

posted

0

Thanks Ben, That was really nice & interesting to know..!!