The Artima Developer Community
Sponsored Link

Conversion Diversion
A Simulation of the Java Virtual Machine

Advertisement

The Conversion Diversion applet, included below, demonstrates a Java virtual machine executing a sequence of bytecodes that performs type conversion. This applet accompanies Chapter 11, "Type Conversion," 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 Convert() method of the class shown below:

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

    static void Convert() {

        byte imByte = 0;
        int imInt = 125;

        for (;;) {
            ++imInt;
            imByte = (byte) imInt;

            imInt *= -1;
            imByte = (byte) imInt;
            imInt *= -1;
        }
    }
}

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

 0 iconst_0    // Push int constant 0.
 1 istore_0    // Pop to local variable 0, which is
               // imByte: byte imByte = 0;
 2 bipush 125  // Expand byte constant 125 to int and push.
 4 istore_1    // Pop to local variable 1, which
               // is imInt: int imInt = 125;
 5 iinc 1 1    // Increment local variable 1 (imInt) by 1: ++imInt;
 8 iload_1     // Push local variable 1 (imInt).
 9 i2b         // Truncate and sign extend top of stack so it
               // has a valid byte value.
10 istore_0    // Pop to local variable 0 (imByte):
               // imByte = (byte) imInt;
11 iload_1     // Push local variable 1 (imInt) again.
12 iconst_m1   // Push integer -1.
13 imul        // Pop top two ints, multiply, push result.
14 istore_1    // Pop result of multiply to local variable 1 (imInt):
               // imInt *= -1;
15 iload_1     // Push local variable 1 (imInt).
16 i2b         // Truncate and sign extend top of stack so it has
               // a valid byte value.
17 istore_0    // Pop to local variable 0 (imByte):
               // imByte = (byte) imInt;
18 iload_1     // Push local variable 1 (imInt) again.
19 iconst_m1   // Push integer -1.
20 imul        // Pop top two ints, multiply, push result.
21 istore_1    // Pop result of multiply to local variable 1 (imInt):
               // imInt *= -1;
22 goto 5      // Jump back to the iinc instruction: for (;;) {}

The Convert() method demonstrates the manner in which the Java virtual machine converts from int to byte. imInt starts out as 125. Each pass through the while loop, it is incremented and converted to a byte. Then it is multiplied by -1 and again converted to a byte. The simulation quickly shows what happens at the edges of the valid range for the byte type.

The maximum value for a byte is 127. The minimum value is -128. Values of type int that are within this range convert directly to byte. As soon as the int gets beyond the valid range for byte, however, things get interesting.

The Java virtual machine converts an int to a byte by truncating and sign extending. The highest order bit, the "sign bit," of longs, ints, shorts, and bytes indicate whether or not the integer value is positive or negative. If the sign bit is zero, the value is positive. If the sign bit is one, the value is negative. Bit 7 of a byte value is its sign bit. To convert an int to a byte, bit 7 of the int is copied to bits 8 through 31. This produces an int that has the same numerical value that the int's lowest order byte would have if it were interpreted as a byte type. After the truncation and sign extension, the int will contain a valid byte value.

The simulation applet shows what happens when an int that is just beyond the valid range for byte types gets converted to a byte. For example, when the imInt variable has a value of 128 (0x00000080) and is converted to byte, the resulting byte value is -128 (0xffffff80). Later, when the imInt variable has a value of -129 (0xffffff7f) and is converted to byte, the resulting byte value is 127 (0x0000007f).

To drive the Conversion Diversion 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 Conversion Diversion applet.


Sponsored Links



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