Java Reference
In-Depth Information
A widening primitive conversion from
float
to
double
that is not
strictfp
may lose information
about the overall magnitude of the converted value.
A widening 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
A widening conversion of a signed integer value to an integral type
T
simply sign-extends
the two's-complement representation of the integer value to fill the wider format.
A widening conversion of a
char
to an integral type
T
zero-extends the representation of the
char
value to fill the wider format.
Despite the fact that loss of precision may occur, a widening primitive conversion never
Example 5.1.2-1. Widening Primitive Conversion
class Test {
public static void main(String[] args) {
int big = 1234567890;
float approx = big;
System.out.println(big - (int)approx);
}
}
This program prints:
-46
thus indicating that information was lost during the conversion from type
int
to type
float
because values of type
float
are not precise to nine significant digits.
5.1.3. Narrowing Primitive Conversion
22 specific conversions on primitive types are called the
narrowing primitive conversions
:
•
short
to
byte
or
char
•
char
to
byte
or
short
•
int
to
byte
,
short
, or
char
•
long
to
byte
,
short
,
char
, or
int