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 widening primitive conversion (§ 5.1.2 )
• a narrowing primitive conversion (§ 5.1.3 )
• a widening and narrowing primitive conversion (§ 5.1.4 )
• a widening reference conversion (§ 5.1.5 ) optionally followed by either an unbox-
ing conversion (§ 5.1.8 ) or an unchecked conversion (§ 5.1.9 )
• a narrowing reference conversion (§ 5.1.6 ) optionally followed by either an unbox-
ing conversion (§ 5.1.8 ) or an unchecked conversion (§ 5.1.9 )
• a boxing conversion (§ 5.1.7 ) optionally followed by a widening reference conver-
sion (§ 5.1.5 )
• an unboxing conversion (§ 5.1.8 ) optionally followed by a widening primitive con-
version (§ 5.1.2 ) .
Value set conversion (§ 5.1.13 ) is applied after the type conversion.
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-
erence type if no compile-time error occurs given the rules in § 5.5.1 .
The following tables enumerate which conversions are used in certain casting conversions.
Each conversion is signified by a symbol:
• - signifies no casting conversion allowed
Search WWH ::




Custom Search