next up previous
Next: Explicit type conversion (casting) Up: Unit 04 Previous: Expressions that involve different

Assignments between different primitive numeric types

A value of a given type cannot be assigned to a variable of a data type with a smaller size, because we would risk to lose information. Moreover, a floating point value cannot be assigned to an integer variable.

The following table describes whether the assignment a=b is legal, for each possible type for a (on the rows) and b (on the columns).

a=b byte short int long float double
byte OK ERROR ERROR ERROR ERROR ERROR
short OK OK ERROR ERROR ERROR ERROR
int OK OK OK ERROR ERROR ERROR
long OK OK OK OK ERROR ERROR
float OK OK OK OK OK ERROR
double OK OK OK OK OK OK

Example:


next up previous
Next: Explicit type conversion (casting) Up: Unit 04 Previous: Expressions that involve different