Java Reference
In-Depth Information
5.1.5. Widening Reference Conversion
A
widening reference
conversion exists from any reference type
S
to any reference type
T
,
Widening reference conversions never require a special action at run time and therefore
never throw an exception at run time. They consist simply in regarding a reference as hav-
ing some other type in a manner that can be proved correct at compile time.
5.1.6. Narrowing Reference Conversion
Six kinds of conversions are called the
narrowing reference conversions
:
• From any reference type
S
to any reference type
T
, provided that
S
is a proper su-
pertype of
T
(§
4.10
).
An important special case is that there is a narrowing reference conversion from
• From any class type
C
to any non-parameterized interface type
K
, provided that
C
is not
final
and does not implement
K
.
• From any interface type
J
to any non-parameterized class type
C
that is not
final
.
• From any interface type
J
to any non-parameterized interface type
K
, provided that
J
is not a subinterface of
K
.
• From the interface types
Cloneable
and
java.io.Serializable
to any array type
T
[]
.
• From any array type
SC
[]
to any array type
TC
[]
, provided that
SC
and
TC
are ref-
erence types and there is a narrowing reference conversion from
SC
to
TC
.
Such conversions require a test at run time to find out whether the actual reference value is
a legitimate value of the new type. If not, then a
ClassCastException
is thrown.
5.1.7. Boxing Conversion
Boxing conversion converts expressions of primitive type to corresponding expressions of
reference type. Specifically, the following nine conversions are called the
boxing conver-
sions
:
• From type
boolean
to type
Boolean
• From type
byte
to type
Byte
• From type
short
to type
Short
• From type
char
to type
Character
• From type
int
to type
Integer