Contents | Prev | Next | IndexThe JavaTM Virtual Machine Specification


checkcast

Operation Check whether object is of given type

checkcast
indexbyte1
indexbyte2


Forms checkcast = 192 (0xc0)

Stack ..., objectref ..., objectref

Description The objectref must be of type reference. The unsigned indexbyte1 and indexbyte2 are used to construct an index into the constant pool of the current class (§3.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The constant pool item at the index must be a CONSTANT_Class (§4.4.1), a symbolic reference to a class, array, or interface type. The symbolic reference is resolved (§5.1).

If objectref is null or can be cast to the resolved class, array, or interface type, the operand stack is unchanged; otherwise, the checkcast instruction throws a ClassCastException.

The following rules are used to determine whether an objectref that is not null can be cast to the resolved type: if S is the class of the object referred to by objectref and T is the resolved class, array, or interface type, checkcast determines whether objectref can be cast to type T as follows:

S cannot be an interface type, because there are no instances of interfaces, only instances of classes and arrays.

Linking Exceptions During resolution of the CONSTANT_Class constant pool item, any of the exceptions documented in §5.1 can be thrown.

Runtime Exception Otherwise, if objectref cannot be cast to the resolved class, array, or interface type, the checkcast instruction throws a ClassCast-Exception.

Notes The checkcast instruction is very similar to the instanceof instruction. It differs in its treatment of null, its behavior when its test fails (checkcast throws an exception, instanceof pushes a result code), and its effect on the operand stack.


Contents | Prev | Next | Index

Java Virtual Machine Specification

Copyright © 1996, 1997 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to [email protected]