As it states from oracle
Reference from Oracle Docs
Widening Primitive Conversion 19 specific conversions on primitive types are called the widening primitive conversions:
- byte to short, int, long, float, or double
- short to int, long, float, or double
- char to int, long, float, or double
- int to long, float, or double
- long to float or double?
- float to double
If a float has 32 bits and a long has 64 how is that considered widening? Shouldn't this be considered narrowing?
If you look at the matter in simple terms, it is about how data has been represented by original designers.
ideally bit depth of long(64) is larger than float(32). But float data has represented using scientific notion which allows to represent considerably much larger range
Ex: 300[original number] : 3×102 [scientific representation]
Long : -2^63 to 2^63-1
Float : (-3.4)*10^38 to (3.4)*10^38
Notice the Long(power of two) Vs Float(power of ten) representational difference here which allow float to have higher range
hope this is helpful
The range of values that can be represented by a
float
ordouble
is much larger than the range that can be represented by along
. Although one might lose significant digits when converting from along
to afloat
, it is still a "widening" operation because the range is wider.From the Java Language Specification, §5.1.2:
Note that a
double
can exactly represent every possibleint
value.It's considered widening because
float
anddouble
can represent larger values thanlong
. You may lose precision, but it will be possible to represent the value (at least approximately).It is considered widening because the numbers that can be represented by a float is larger than numbers that can represented by long. Just because float uses 32 bit precision does not mean the numbers it can represent are limited to 2^32.
For instance the float
(float)Long.MAX_VALUE+(float)Long.MAX_VALUE
is larger thanLong.MAX_VALUE
, even though the float has less precision that the long.