The Artima Developer Community
Sponsored Link

Objects and Arrays
Java's Bytecodes that Deal with Objects and Arrays
by Bill Venners
First Published in JavaWorld, December 1996

<<  Page 2 of 4  >>

Advertisement

Opcodes for objects
Instantiation of new objects is accomplished via the new opcode. Two one-byte operands follow the new opcode. These two bytes are combined to form a 16-bit index into the constant pool. The constant pool element at the specified offset gives information about the class of the new object. The JVM creates a new instance of the object on the heap and pushes the reference to the new object onto the stack, as shown below.

Object creation
Opcode Operand(s) Description
new indexbyte1, indexbyte2 creates a new object on the heap, pushes reference

The next table shows the opcodes that put and get object fields. These opcodes, putfield and getfield, operate only on fields that are instance variables. Static variables are accessed by putstatic and getstatic, which are described later. The putfield and getfield instructions each take two one-byte operands. The operands are combined to form a 16-bit index into the constant pool. The constant pool item at that index contains information about the type, size, and offset of the field. The object reference is taken from the stack in both the putfield and getfield instructions. The putfield instruction takes the instance variable value from the stack, and the getfield instruction pushes the retrieved instance variable value onto the stack.

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.

Type checking
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

<<  Page 2 of 4  >>


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us