Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Objects and arrays

A look at the bytecodes that deal with objects and arrays in the Java virtual machine

  • Print
  • Feedback

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.

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


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.

  • Print
  • Feedback

Resources
  • Previous Under The Hood articles
  • The lean, mean virtual machine -- Gives an introduction to the Java virtual machine. Look here to see how the garbage collected heap fits in with the other parts of the Java Virtual Machine.
  • The Java class file lifestyle -- Gives an overview to the Java class file, the file format into which all Java programs are compiled.
  • Java's garbage-collected heap -- Gives an overview of garbage collection in general and the garbage-collected heap of the Java virtual machine in particular.
  • Bytecode basics -- Introduces the bytecodes of the Java Virtual Machine, and discusses primitive types, conversion operations, and stack operations in particular.
  • Floating Point Arithmetic -- Describes the Java Virtual Machine's floating point support and the bytecodes that perform floating point operations.
  • Logic and Integer Arithmetic -- Describes the Java Virtual Machine's support for logical and integer arithmetic, and the relevant bytecode instructions.