Converting between primitive types
The maximum numeric value that a numeric type can hold depends on the number of bits allocated to it. The following are the number of bits for each numeric type of representation:
byte
: 8 bitschar
: 16 bitsshort
: 16 bitsint
: 32 bitslong
: 64 bitsfloat
: 32 bitsdouble
: 64 bits
When a value of one numeric type is assigned to a variable of another numeric type and the new type can hold a bigger number, such a conversion is called a widening conversion. Otherwise, it is a narrowing conversion, which usually requires typecasting, using a cast
operator.
Widening conversion
According to the Java Language Specification, there are 19 widening primitive type conversions:
byte
toshort
,int
,long
,float
, ordouble
short
toint
,long
,float
, ordouble
char
toint
,long
,float
, ordouble
int
tolong
,float
, ordouble
long
tofloat
ordouble
float
todouble
During widening conversion...