Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Objects and arrays

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

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Welcome to another edition of Under The Hood. This column focuses on Java's underlying technologies. It aims to give developers a glimpse of the mechanisms that make their Java programs run. This month's article takes a look at the bytecodes that deal with objects and arrays.

Object-oriented machine

The Java virtual machine (JVM) works with data in three forms: objects, object references, and primitive types. Objects reside on the garbage-collected heap. Object references and primitive types reside either on the Java stack as local variables, on the heap as instance variables of objects, or in the method area as class variables.

In the Java virtual machine, memory is allocated on the garbage-collected heap only as objects. There is no way to allocate memory for a primitive type on the heap, except as part of an object. If you want to use a primitive type where an Object reference is needed, you can allocate a wrapper object for the type from the java.lang package. For example, there is an Integer class that wraps an int type with an object. Only object references and primitive types can reside on the Java stack as local variables. Objects can never reside on the Java stack.

The architectural separation of objects and primitive types in the JVM is reflected in the Java programming language, in which objects cannot be declared as local variables. Only object references can be declared as such. Upon declaration, an object reference refers to nothing. Only after the reference has been explicitly initialized -- either with a reference to an existing object or with a call to new -- does the reference refer to an actual object.

In the JVM instruction set, all objects are instantiated and accessed with the same set of opcodes, except for arrays. In Java, arrays are full-fledged objects, and, like any other object in a Java program, are created dynamically. Array references can be used anywhere a reference to type Object is called for, and any method of Object can be invoked on an array. Yet, in the Java virtual machine, arrays are handled with special bytecodes.

As with any other object, arrays cannot be declared as local variables; only array references can. Array objects themselves always contain either an array of primitive types or an array of object references. If you declare an array of objects, you get an array of object references. The objects themselves must be explicitly created with new and assigned to the elements of the array.

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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

source codeBy Anonymous on April 30, 2009, 2:09 amcomplete source code searching

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
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.