Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Widening conversion

jeff mutonho
Ranch Hand
Posts: 271
In the RHE book on page 108 , they have a diagram which shows that you can do widening conversion as shown below
-->(int)--->(long)--->(float)--->(double)
with the arrows meaning "can be widened to".
A long is 64 bits whilst a float is 32 bits.So how then is it possible to do a "widening" conversion from a long which is 64 bits in size to a float which is 32 bits in size?Where is the widening there?I would think one would need a cast to do so?What am I missing.

Jose Botella
Ranch Hand
Posts: 2120
Welcome to the Ranch jeff.
This from JLS 5.1.2 Widening primitive converstions:

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (�4.2.4).

Rory French
Ranch Hand
Posts: 97
Also remember that the bit representation of floating point numbers is different to that of the integer types. Floating point types have signed bits, mantissas and exponents, so they cover a broader numeric range than integers.
type float has a broader numeric range than type long, so widening conversion is possible without an explicit cast, but with a possible loss of precision (as mentioned by Jose above):
Here's a simple program to illustrate the numeric ranges of long relative to float :

(I'm using -Float.MAX_VALUE instead of Float.MIN_VALUE because Float.MIN_VALUE represents the smallest positive floating point value, and not the most negative floating point number.)
[ April 30, 2003: Message edited by: Rory French ]

jeff mutonho
Ranch Hand
Posts: 271
but it still isn't clear.I thought the idea of calling it "widening" conversion was because the new type you're converting to is wider than the old type so one does not have to cast.Eg when you do a conversion of a byte to an int because you can fit the byte's 8 bits into an int's 32 bits because because there is enough room to do so.Now with the long and float I'm still finding the explanation unsatistfactory.Yes the spec says you will lose precision , but YOU can do it.So why can't we do it with , eg a short and a byte ie converting a short to byte?
short b=7; //wider than
byte s=7; // this
b=s; //convert wider to narrower : PROBLEM!
Why then do we get a compile error "Possible loss of precision"
But this does not give a compile error.
long l = 10L; //wider than
float f = 10.0F; //this
f=l; //convert wider to narrower :NO PROBLEM!

jeff mutonho
Ranch Hand
Posts: 271
a buddy here is explaining the effects of the representation of the floats...it makes sense to think about it that way....I think the books should not just generelize "conversion" based on size of the data types...
Thanx to everyone who responded to my question.