The Artima Developer Community
Sponsored Link

Three Dimensional Array
A Simulation of the Java Virtual Machine

Advertisement

The Three-Dimensional Array applet, included below, demonstrates a Java virtual machine executing a sequence of bytecodes. This applet accompanies Chapter 15, "Objects and Arrays," of Inside the Java 2 Virtual Machine.

For some reason, your browser won't let you view this way cool Java applet.

The bytecode sequence in the simulation was generated by javac for the initAnArray() method of the class shown below:

// On CD-ROM in file opcodes/ex1/ThreeDTree.java
class ThreeDTree {

    static void initAnArray() {

        int[][][] threeD = new int[5][4][3];

        for (int i = 0; i < 5; ++i) {
            for (int j = 0; j < 4; ++j) {
                for (int k = 0; k < 3; ++k) {
                    threeD[i][j][k] = i + j + k;
                }
            }
        }
    }
}

The bytecodes generated by javac for initAnArray() are shown below:

 0 iconst_5      // Push constant int 5.
 1 iconst_4      // Push constant int 4.
 2 iconst_3      // Push constant int 3.
 3 multianewarray #2 dim #3 
                 // Create a new multi-dimensional array using
                 // constant pool entry #2 as the class (which
                 // is [[[I, an 3D array of ints) with a
                 // dimension of 3.
 7 astore_0      // Pop object ref into local variable 0:
                 // int threeD[][][] = new int[5][4][3];
 8 iconst_0      // Push constant int 0.
 9 istore_1      // Pop int into local variable 1: int i = 0;
10 goto 54       // Go to section of code that tests outer loop.
13 iconst_0      // Push constant int 0.
14 istore_2      // Pop int into local variable 2: int j = 0;
15 goto 46       // Go to section of code that tests middle loop.
18 iconst_0      // Push constant int 0.
19 istore_3      // Pop int into local variable 3: int k = 0;
20 goto 38       // Go to section of code that tests inner loop.
23 aload_0       // Push object ref from local variable 0.
24 iload_1       // Push int from local variable 1 (i).
25 aaload        // Pop index and arrayref, push object ref
                 // at arrayref[index] (gets threeD[i]).
26 iload_2       // Push int from local variable 2 (j).
27 aaload        // Pop index and arrayref, push object ref
                 // at arrayref[index] (gets threeD[i][j]).
28 iload_3       // Push int from local variable 3 (k).
                 // Now calculate the int that will be assigned
                 // to threeD[i][j][k]
29 iload_1       // Push int from local variable 1 (i).
30 iload_2       // Push int from local variable 2 (j).
31 iadd          // Pop two ints, add them, push int result (i + j).
32 iload_3       // Push int from local variable 3 (k).
33 iadd          // Pop two ints, add them, push int
                 // result (i + j + k).
34 iastore       // Pop value, index, and arrayref; assign
                 // arrayref[index] = value:
                 // threeD[i][j][k] = i + j + k;
35 iinc 3 1      // Increment by 1 the int in local variable 3: ++k;
38 iload_3       // Push int from local variable 3 (k).
39 iconst_3      // Push constant int 3.
40 if_icmplt 23  // Pop right and left ints, jump if left < right:
                 // for (...; k < 3;...)
43 iinc 2 1      // Increment by 1 the int in local variable 2: ++j;
46 iload_2       // Push int from local variable 2 (j).
47 iconst_4      // Push constant int 4.
48 if_icmplt 18  // Pop right and left ints, jump if left < right:
                 // for (...; j < 4;...)
51 iinc 1 1      // Increment by 1 the int in local variable 1: ++i;
54 iload_1       // Push int from local variable 1 (i).
55 iconst_5      // Push constant int 5.
56 if_icmplt 13  // Pop right and left ints, jump if left < right:
                 // for (...; i < 5;...)
59 return

The initAnArray() method merely allocates and initializes a three-dimensional array. This simulation demonstrates how the Java virtual machine handles multidimensional arrays. In response to the multianewarray instruction, which in this example requests the allocation of a three-dimensional array, the Java virtual machine creates a tree of one-dimensional arrays. The reference returned by the multianewarray instruction refers to the base one-dimensional array in the tree. In the initAnArray() method, the base array has five components--threeD[0] through threeD[4]. Each component of the base array is itself a reference to a one-dimensional array of four components, accessed by threeD[0][0] through threeD[4][3]. The components of these five arrays are also references to arrays, each of which has three components. These components are ints, the elements of this multidimensional array, and they and they are accessed by threeD[0][0][0] through threeD[4][3][2].

In response to the multianewarray instruction in the initAnArray() method, the Java virtual machine creates one five-dimensional array of arrays, five four-dimensional arrays of arrays, and twenty three-dimensional arrays of ints. The Java virtual machine allocates these 26 arrays on the heap, initializes their components such that they form a tree, and returns the reference to the base array.

To assign an int value to an element of the three-dimensional array, the Java virtual machine uses aaload to get a component of the base array. Then the virtual machine uses aaload again on this component--which is itself an array of arrays--to get a component of the branch array. This component is a reference to a leaf array of ints. Finally the virtual machine uses iastore to assign an int value to the element of the leaf array. The Java virtual machine uses multiple one-dimensional array accesses to accomplish operations on multidimensional arrays.

To drive the Three-Dimensional Array simulation, use the Step, Reset, Run, and Stop buttons. Each time you press the Step button, the simulator will execute the instruction pointed to by the pc register. If you press the Run button, the simulation will continue with no further coaxing on your part until you press the Stop button. To start the simulation over, press the Reset button. For each step of the simulation, a panel at the bottom of the applet contains an explanation of what the next instruction will do. Happy clicking.


Click here to view a page of links to the source code of the Three Dimensional Array applet.


Sponsored Links



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