If the type of an expression is a primitive type, then the value of the expression is
of that same primitive type. But if the type of an expression is a reference type,
then the class of the referenced object, or even whether the value is a reference to
an object rather than null
, is not necessarily known at compile time. There are a
few places in the Java language where the actual class of a referenced object
affects program execution in a manner that cannot be deduced from the type of the
expression. They are as follows:
o.m(
...)
is chosen based on the methods that are part of the class or interface that is the type of o
. For instance methods, the class of the object referenced by the run-time value of o
participates because a subclass may override a specific method already declared in a parent class so that this overriding method is invoked. (The overriding method may or may not choose to further invoke the original overridden m
method.)
instanceof
operator (§15.19.2). An expression whose type is a reference type may be tested using instanceof
to find out whether the class of the object referenced by the run-time value of the expression is assignment compatible (§5.2) with some other reference type.
[]
to be treated as a subtype of T[]
if S is a subtype of T, but this requires a run-time check for assignment to an army component, similar to the check performed for a cast.
catch
clause only if the class of the thrown exception object is an instanceof
the type of the formal parameter of the catch
clause.
The first two of the cases just listed ought never to result in detecting a type error. Thus, a Java run-time type error can occur only in these situations:
ClassCastException
is thrown.
ArrayStoreException
is thrown.
catch
handler (§11.3); in this case the thread of control that encountered the exception first invokes the method uncaughtException
(§20.21.31) for its thread group and then terminates.