Thursday, July 25, 2013

Automatic Type Conversions in Assignments

When the type of the result of an arithmetic expression on the right of an assignment operator differs from the type of the variable on the left, an automatic cast will be applied to the result as long as there is no possibility of losing information in Java program. If you think of the basic types that we have seen so far as being in the sequence

byte => short => int => long => float => double

then an automatic conversion will be made as long as it is upwards through the sequence of types, that is, from left to right. If you want to go in the opposite direction, from type double to type float or long, for example, then you must insert an explicit cast into your code for the result of the expression on< the right of the assignment operator.

No comments:

Post a Comment

Thanks for Commenting......