The Type Cast Operator
You’ve already seen how enclosing a type inside a set of parentheses is used to declare the return and argument types when declaring and defining methods. It serves a different purpose when used inside expressions.
The last division operation from Program 4.5 that reads as follows introduces the type cast operator:
f2 = (float) i2 / 100; // type cast operator
The type cast operator has the effect of converting the value of the variable i2 to type float for purposes of evaluating the expression. In no way does this operator permanently affect the value of the variable i2; it is a unary operator that behaves like other unary operators. Just as the expression -a has no permanent effect on the value of a, neither does the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access