doublevalue to a
longvariable without an explicit narrowing conversion is because some information may be lost during the conversion. Conversely, the reason why Java lets you assign a
longvalue to a
doublevariable without an explicit widening conversion is because no information is ever lost during the conversion.
double d = 604444444444444444L; // implicit widening conversion
// prints 6.0444444444444442E17
This is cautioned in JLS 5.1.2 Widening Primitive Conversion:
Conversion of anRecall that
float, or of a
double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value.
Long.SIZE == 64and
Float.SIZE == 32, and yet going from
floatis in fact a widening conversion. The words "widening/narrowing" here has nothing to do with bit size and/or information capacity at all.
Thus, the following two claims are both false!!!
A widening conversion goes from a smaller to a larger data type in terms of bits
... and thus no information is ever lost
And by the way, yes, that's a World Cup reference. No, I don't watch soccer.