Java Reference
In-Depth Information
• Widening reference conversions
• Narrowing reference conversions
• Boxing conversions
• Unboxing conversions
• Unchecked conversions
• Capture conversions
• String conversions
• Value set conversions
There are five conversion contexts in which conversion of expressions may occur. Each
context allows conversions in some of the categories named above but not others. The term
“conversion” is also used to describe the process of choosing a specific conversion for such
a context. For example, we say that an expression that is an actual argument in a method in-
vocation is subject to “method invocation conversion,” meaning that a specific conversion
will be implicitly chosen for that expression according to the rules for the method invoca-
tion argument context.
One conversion context is the operand of a numeric operator such as + or * . The conversion
process for such operands is called numeric promotion . Promotion is special in that, in the
case of binary operators, the conversion chosen for one operand may depend in part on the
type of the other operand expression.
This chapter first describes the eleven categories of conversions (§ 5.1 ) , including the spe-
cial conversions to String allowed for the string concatenation operator + 15.18.1 ). Then
the five conversion contexts are described:
• Assignment conversion (§ 5.2 , § 15.26 ) converts the type of an expression to the
type of a specified variable.
Assignment conversion may cause an OutOfMemoryError (as a result of boxing con-
version (§ 5.1.7 ) ), a NullPointerException (as a result of unboxing conversion
5.1.8 ) ), or a ClassCastException (as a result of an unchecked conversion (§ 5.1.9 )) to
be thrown at run time.
• Method invocation conversion (§ 5.3 , § 15.9 , § 15.12 ) is applied to each argument in
a method or constructor invocation and, except in one case, performs the same
conversions that assignment conversion does.
Method invocation conversion may cause an OutOfMemoryError (as a result of box-
ing conversion (§ 5.1.7 ) ), a NullPointerException (as a result of unboxing conversion
Search WWH ::




Custom Search