instanceof 0xc1
Pops a reference; pushes 1 if it is of a type, else 0.
- Opcode
0xc1(193)- Family
- Objects and types
- Operands
- 2 bytes: constant pool index
- Length
- 3 bytes
- Operand stack
…, objectref → …, result (int)- Can throw
- nothing at run time (beyond linking errors)
What it does
Pops a reference and pushes 1 if it is an instance of the type, 0 if not. null gives 0.
A type pattern (o instanceof String s, Java 16) compiles to the same instanceof, then a checkcast and a store into the new variable.
How often javac uses it
In JDK 25.0.5's java.base module, instanceof appears 2,895 times in 1,052 of its 7,391 classes: 0.15% of all instructions, the 77th most common of 194 opcodes in use. Some of the methods that use it: java.io.DataInputStream.readLine, java.io.DataOutputStream.writeUTF, java.io.File.equals, java.io.FileInputStream.transferTo.
Examples
From the sample classes, compiled by javac 25 with -g. Each Java line is shown above the bytecode it compiled to; instanceof is highlighted.
static int oldStyle(Object o) in InstanceofPattern, compiled by javac 25static String cast(Object o) in Tour, compiled by javac 25Related instructions
checkcast Checks that the reference on top of the stack is of a type (throws ClassCastException if not).ifeq Pops an int; jumps if it is equal to zero.
Specification: JVMS §6.5, instanceof (Java SE 25).