CONVERSIONS AND PROMOTIONS
Casting Conversion
5.5
If a cast to a reference type is not a compile time error, there are two cases:
The cast can be determined to be correct at compile time. A cast from the
compile time type
S
to compile time type
T
is correct at compile time if and
only if
S
can be converted to
T
by assignment conversion ( 5.2).
The cast requires a run time validity check. If the value at run time is
null
,
then the cast is allowed. Otherwise, let
R
be the class of the object referred to
by the run time reference value, and let
T
be the type named in the cast opera
tor. A cast conversion must check, at run time, that the class
R
is assignment
compatible with the type
T
, using the algorithm specified in 5.2 but using the
class
R
instead of the compile time type
S
as specified there. (Note that
R
can
not be an interface when these rules are first applied for any given cast, but
R
may be an interface if the rules are applied recursively because the run time
reference value refers to an array whose element type is an interface type.)
This modified algorithm is shown here:
N
If
R
is an ordinary class (not an array class):
O
If
T
is a class type, then
R
must be either the same class ( 4.3.4) as
T
or a
subclass of
T
, or a run time exception is thrown.
O
If
T
is an interface type, then
R
must implement ( 8.1.4) interface
T
, or a
run time exception is thrown.
O
If
T
is an array type, then a run time exception is thrown.
N
If
R
is an interface:
O
If
T
is a class type, then
T
must be
Object
( 4.3.2, 20.1), or a run time
exception is thrown.
O
If
T
is an interface type, then
R
must be either the same interface as
T
or a
subinterface of
T
, or a run time exception is thrown.
O
If
T
is an array type, then a run time exception is thrown.
N
If
R
is a class representing an array type
RC[]
that is, an array of compo
nents of type
RC
:
O
If
T
is a class type, then
T
must be
Object
( 4.3.2, 20.1), or a run time
exception is thrown.
O
If
T
is an interface type, then a run time exception is thrown unless
T
is
the interface type
Cloneable
, the only interface implemented by arrays
(this case could slip past the compile time checking if, for example, a ref
erence to an array were stored in a variable of type
Object
).
69
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved