EXPRESSIONS
Expressions and Run Time Checks
15.4
Method invocation ( 15.11). The particular method used for an invocation
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 spe
cific 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.)
The
instanceof
operator ( 15.19.2). An expression whose type is a refer
ence 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 com
patible ( 5.2) with some other reference type.
Casting ( 5.4, 15.15). The class of the object referenced by the run time
value of the operand expression might not be compatible with the type speci
fied by the cast. For reference types, this may require a run time check that
throws an error if the class of the referenced object, as determined at run time,
is not assignment compatible ( 5.2) with the target type.
Assignment to an array component of reference type ( 10.10, 15.12,
15.25.1). 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 army component, similar to the check performed for a cast.
Exception handling ( 14.18). An exception is caught by a
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:
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 ( 5.4, 15.15); in this case a
ClassCastException
is thrown.
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 run time component type of the array ( 10.10, 15.12,
15.25.1); in this case an
ArrayStoreException
is thrown.
When an exception is not caught by any
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.
303
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