Java Reference
In-Depth Information
other reference type.
the operand expression might not be compatible with the type specified by the cast.
For reference types, this may require a run-time check that throws an exception if
the class of the referenced object, as determined at run time, is not assignment
The type-checking rules allow the array type
S
[]
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 array
component, similar to the check performed for a cast.
class of the thrown exception object is an
instanceof
the type of the formal paramet-
er of the
catch
clause.
Situations where the class of an object is not statically known may lead to run-time type
errors.
In addition, there are situations where the statically known type may not be accurate at
run time. Such situations can arise in a program that gives rise to compile-time unchecked
warnings. Such warnings are given in response to operations that cannot be statically guar-
anteed to be safe, and cannot immediately be subjected to dynamic checking because they
gram execution may detect inconsistencies and result in run-time type errors.
A run-time type error can occur only in these situations:
• In a cast, when the actual class of the object referenced by the value of the operand
expression is not compatible with the target type specified by the cast operator
• In an automatically generated cast introduced to ensure the validity of an operation
• In an assignment to an array component of reference type, when the actual class of
the object referenced by the value to be assigned is not compatible with the actual
rayStoreException
is thrown.
this case the thread of control that encountered the exception first attempts to in-