Java Reference
In-Depth Information
From
To
byte
short
short , int , long , float , or double
int , long , float , or double
char
int
long
int , long , float , or double
long , float , or double
float or double
double
float
FIGURE 2.5
Java widening conversions
Although widening conversions do not lose any information
about the magnitude of a value, the widening conversions that result
in a floating point value can lose precision. When converting from
an int or a long to a float , or from a long to a double , some of the
least significant digits may be lost. In this case, the resulting floating
point value will be a rounded version of the integer value, following the rounding
techniques defined in the IEEE 754 floating point standard.
Narrowing conversions are more likely to lose information than widening con-
versions are. They often go from one type to a type that uses less space to store
a value, and therefore some of the information may be compromised. Narrowing
conversions can lose both numeric magnitude and precision. Therefore, in gen-
eral, they should be avoided. Figure 2.6 lists the Java narrowing conversions.
An exception to the space-shrinking situation in narrowing conversions is when
we convert a byte (8 bits) or short (16 bits) to a char (16 bits). These are still
KEY CONCEPT
Narrowing conversions should be
avoided because they can lose
information.
From
To
byte
char
short
char
byte or char
byte or short
int
long
byte , short , or char
byte , short , char , or int
float
byte , short , char , int , or long
double
byte , short , char , int , long , or float
FIGURE 2.6
Java narrowing conversions
 
 
Search WWH ::




Custom Search