Java Reference
In-Depth Information
5.5. Casting Conversion
Casting conversion
is applied to the operand of a cast operator (§
15.16
): the type of the
operand expression must be converted to the type explicitly named by the cast operator.
Casting contexts allow the use of one of:
• an identity conversion (§
5.1.1
)
• a narrowing reference conversion (§
5.1.6
) optionally followed by either an unbox-
The compile-time legality of a casting conversion is as follows:
• An expression of a primitive type may undergo casting conversion to another
primitive type, by an identity conversion (if the types are the same), or by a widen-
ing primitive conversion, or by a narrowing primitive conversion, or by a widening
and narrowing primitive conversion.
• An expression of a primitive type may undergo casting conversion to a reference
type without error, by boxing conversion.
• An expression of a reference type may undergo casting conversion to a primitive
type without error, by unboxing conversion.
• An expression of a reference type may undergo casting conversion to another ref-
The following tables enumerate which conversions are used in certain casting conversions.
Each conversion is signified by a symbol:
• - signifies no casting conversion allowed