The Artima Developer Community
Under The Hood | Book List | Print | Email | First Page | Previous | Next
Sponsored Link

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

Page 1 of 4  >>


All Java programs are compiled into class files that contain bytecodes, the machine language of the Java virtual machine. This article takes a look at the bytecodes that manipulate objects and arrays.

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.

Page 1 of 4  >>

Under The Hood | Book List | Print | Email | First Page | Previous | Next

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use