Objects and arrays
A look at the bytecodes that deal with objects and arrays in the Java virtual machine
By Bill Venners, JavaWorld.com, 12/01/96
Page 2 of 3
Accessing instance variables
| Opcode |
Operand(s) |
Description |
putfield |
indexbyte1, indexbyte2 |
set field, indicated by index, of object to value (both taken from stack) |
getfield |
indexbyte1, indexbyte2 |
pushes field, indicated by index, of object (taken from stack) |
Class variables are accessed via the getstatic and putstatic opcodes, as shown in the table below. Both getstatic and putstatic
take two one-byte operands, which are combined by the JVM to form a 16-bit unsigned offset into the constant pool. The constant
pool item at that location gives information about one static field of a class. Because there is no particular object associated
with a static field, there is no object reference used by either getstatic or putstatic. The putstatic instruction takes the
value to assign from the stack. The getstatic instruction pushes the retrieved value onto the stack.
Accessing class variables
| Opcode |
Operand(s) |
Description |
putstatic |
indexbyte1, indexbyte2 |
set field, indicated by index, of object to value (both taken from stack) |
getstatic |
indexbyte1, indexbyte2 |
pushes field, indicated by index, of object (taken from stack) |
The following opcodes check to see whether the object reference on the top of the stack refers to an instance of the class
or interface indexed by the operands following the opcode. The checkcast instruction throws CheckCastException if the object is not an instance of the specified class or interface. Otherwise, checkcast does nothing. The object reference
remains on the stack and execution is continued at the next instruction. This instruction ensures that casts are safe at run
time and forms part of the JVM's security blanket.
The instanceof instruction pops the object reference from the top of the stack and pushes true or false. If the object is
indeed an instance of the specified class or interface, then true is pushed onto the stack, otherwise, false is pushed onto
the stack. The instanceof instruction is used to implement the instanceof keyword of Java, which allows programmers to test whether an object is an instance of a particular class or interface.
| Opcode |
Operand(s) |
Description |
checkcast |
indexbyte1, indexbyte2 |
Throws ClassCastException if objectref on stack cannot be cast to class at index |
instanceof |
indexbyte1, indexbyte2 |
Pushes true if objectref on stack is an instanceof class at index, else pushes false |
Opcodes for arrays
Instantiation of new arrays is accomplished via the newarray, anewarray, and multianewarray opcodes. The newarray opcode is
used to create arrays of primitive types other than object references. The particular primitive type is specified by a single
one-byte operand following the newarray opcode. The newarray instruction can create arrays for byte, short, char, int, long,
float, double, or boolean.
The anewarray instruction creates an array of object references. Two one-byte operands follow the anewarray opcode and are
combined to form a 16-bit index into the constant pool. A description of the class of object for which the array is to be
created is found in the constant pool at the specified index. This instruction allocates space for the array of object references
and initializes the references to null.