instanceof

Operation

instanceof
indexbyte1
indexbyte2
Determine if object is of given type

Forms
instanceof = 193 (0xc1)

Stack
..., objectref ..., result

Description
The objectref, which must be of type reference, is popped from the operand stack. 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 item at that index in the constant pool must be a CONSTANT_Class (§4.4.1), a symbolic reference to a class, array, or interface. The symbolic reference is resolved (§5.1).

If objectref is not null and is an instance of the resolved class, array, or interface, the instanceof instruction pushes an int result of 1 as an int on the operand stack. Otherwise, it pushes an int result of 0.

The following rules are used to determine whether an objectref that is not null is an instance of 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, instanceof determines whether objectref is an instance of 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.

Notes
The instanceof instruction is fundamentally very similar to the checkcast 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.

Determine if object is of given type

Forms
instanceof = 193 (0xc1)

Stack
..., objectref ..., result

Description
The objectref, which must be of type reference, is popped from the operand stack. 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 item at that index in the constant pool must be a CONSTANT_Class (§4.4.1), a symbolic reference to a class, array, or interface. The symbolic reference is resolved (§5.1).

If objectref is not null and is an instance of the resolved class, array, or interface, the instanceof instruction pushes an int result of 1 as an int on the operand stack. Otherwise, it pushes an int result of 0.

The following rules are used to determine whether an objectref that is not null is an instance of 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, instanceof determines whether objectref is an instance of T as follows: