The Artima Developer Community
Sponsored Link

Play Ball!
A Simulation of the Java Virtual Machine

Advertisement

The Play Ball! Applet, included below, demonstrates a Java virtual machine executing a sequence of bytecodes. This applet accompanies Chapter 17, "Exceptions," 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 playBall method of the class shown below:

// On CD-ROM in file except/ex2/Ball.java
class Ball extends Exception {
}

// On CD-ROM in file except/ex2/Pitcher.java
class Pitcher {

    private static Ball ball = new Ball();

    static void playBall() {
        int i = 0;
        for (;;) {
            try {
                if (i % 4 == 3) {
                    throw ball;
                }
                ++i;
            }
            catch (Ball b) {
                i = 0;
            }
        }
    }
}

The bytecodes generated by javac for the playBall method are shown here:

// The main bytecode sequence for playBall():

 0 iconst_0      // Push constant 0
 1 istore_0      // Pop into local var 0: int i = 0;
                 // The try block starts here (see the
                 // exception table, below).
 2 iload_0       // Push local var 0
 3 iconst_4      // Push constant 4
 4 irem          // Calc remainder of top two operands
 5 iconst_3      // Push constant 3
 6 if_icmpne 13  // Jump if remainder not equal to 3:
                 // if (i % 4 == 3) {
                 // Push the static field at constant pool
                 // location #6, which is the Ball exception eager
                 // to be thrown
 9 getstatic #6 
12 athrow        // Heave it home: throw ball;
13 iinc 0 1      // Increment the int at local var 0 by 1: ++i;
                 // The try block ends here (see the
                 // exception table, below).
16 goto 2        // jump always back to 2: for (;;) {}

// The bytecode sequence for the catch (Ball) clause:

19 pop           // Pop the exception reference because it is unused
20 iconst_0      // Push constant 0
21 istore_0      // Pop into local var 0: i = 0;
22 goto 2        // Jump always back to 2: for (;;) {}

Exception table:
   from   to  target type
     2    16    19   

The playball() method loops forever. Every fourth pass through the loop, playball() throws a Ball and catches it, just because it's fun. Since the try block and the catch clause are both within the endless while loop, the fun never stops. The local variable i starts at 0 and increments each pass through the loop. When the if statement is true, which happens every time i is equal to 3, the Ball exception is thrown.

The Java virtual machine checks the exception table and discovers that there is indeed an applicable entry. The entry's valid range is from 2 to 15, inclusive, and the exception is thrown at pc offset 12. The exception caught by the entry is of class Ball, and the exception thrown is of class Ball. Given this perfect match, the virtual machine pushes the thrown exception object onto the stack, and continues execution at pc offset 19. The catch clause, which starts at offset 19, merely resets int i to 0, and the loop starts over.

To drive the Play Ball! 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 Play Ball! applet.


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use