Home Segments Top Top Previous Next

93: Mainline

If you want to tell Java explicitly to convert a value from one type to another, rather than relying on automatic conversion, you cast the expression. To cast, you prefix the expression with the name of the desired type in parentheses.

If, for example, i is an int and d is a double, you can cast i to a double and d to an int as follows:

(double) i         // A double expression 
(int) d            // An int expression 

Note that the original types of the i and d variables remain undisturbed: i remains an int variable, and d remains a double variable.