JVM:  Arrays

Honors Computer Systems Organization (Prof. Grishman)

Additional notes on integer operations

Other types

JVM provides a full set of load/store/arithmetic operations for integers (as already described), longs (64-bit integers), floats (32-bit floating point), and doubles (64-bit floating point).

Arrays

Arrays are treated as objects by the Java Virtual Machine.  The type of an array is "[elementtype", where elementtype is is type of the elements.  For example, a (1-dimensional) array of integers has type "[I".

A new array of integers is created at execution time by the operation

newarray int
The size of the array should be on the stack before this operation is performed;  the operation leaves on the stack a reference to the array.  References are distinct from the arithmetic types (int, long, etc.).  They are loaded and stored with the operations
aload   var
astore   var
Thus the Java code
int x[]= new int [8];
would be translated (if x was local variable 2) into
ldc        8
newarray   int
astore     2
Elements of an integer array are loaded and stored by the operations
iaload
iastore
In each case, the second element on the stack should be a reference to the array, and the top of the stack the index into the array.  Thus, if local variable 2 holds a reference to array x, the sequence
aload    2
ldc      5
iaload
would put x[5] on top of the stack.

Finally, if an array reference is on the top of the stack, the operation

arraylength
will leave the length of the array on top of the stack.