Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Bytecode basics

A first look at the bytecodes of the Java virtual machine

  • Print
  • Feedback

Page 9 of 10

Opcode Operand(s) Description
astore vindex pops object reference to local variable position vindex
astore_0 (none) pops object reference to local variable position zero
astore_1 (none) pops object reference to local variable position one
astore_2 (none) pops object reference to local variable position two
astore_3 (none) pops object reference to local variable position three

Type conversions

The Java virtual machine has many opcodes that convert from one primitive type to another. No operands follow the conversion opcodes in the bytecode stream. The value to convert is taken from the top of the stack. The JVM pops the value at the top of the stack, converts it, and pushes the result back onto the stack. Opcodes that convert between int, long, float, and double are shown in the following table. There is an opcode for each possible from-to combination of these four types:

Opcode Operand(s) Description
i2l (none) converts int to long
i2f (none) converts int to float
i2d (none) converts int to double
l2i (none) converts long to int
l2f (none) converts long to float
l2d (none) converts long to double
f2i (none) converts float to int
f2l (none) converts float to long
f2d (none) converts float to double
d2i (none) converts double to int
d2l (none) converts double to long
d2f (none) converts double to float

Opcodes that convert from an int to a type smaller than int are shown in the following table. No opcodes exist that convert directly from a long, float, or double to the types smaller than int. Therefore converting from a float to a byte, for example, would require two steps. First the float must be converted to an int with f2i, then the resulting int can be converted to a byte with int2byte.

Opcode Operand(s) Description
int2byte (none) converts int to byte
int2char (none) converts int to char
int2short (none) converts int to short

Although opcodes exist that convert an int to primitive types smaller than int (byte, short, and char), no opcodes exist that convert in the opposite direction. This is because any bytes, shorts, or chars are effectively converted to int before being pushed onto the stack. Arithmetic operations upon bytes, shorts, and chars are done by first converting the values to int, performing the arithmetic operations on the ints, and being happy with an int result. This means that if you add 2 bytes you get an int, and if you want a byte result you must explicitly convert the int result back to a byte. For example, the following code won't compile:

 class BadArithmetic {
        byte addOneAndOne() {
                byte a = 1;
                byte b = 1;
                byte c = a + b;
                return c;
        }
}


When presented with the above code, javac objects with the following remark:

 BadArithmetic.java(7): Incompatible type for declaration. Explicit cast needed to convert int to byte.
                byte c = a + b;
                     ^


To remedy the situation, the Java programmer must explicitly convert the int result of the addition of a + b back to a byte, as in the following code:

 class GoodArithmetic {
        byte addOneAndOne() {
                byte a = 1;
                byte b = 1;
                byte c = (byte) (a + b);
                return c;
        }
}


This makes javac so happy it drops a GoodArithmetic.class file, which contains the following bytecode sequence for the addOneAndOne() method:

  • Print
  • Feedback

Resources
  • Previous Under The Hood articles:
  • The lean, mean virtual machine -- Gives an introduction to the Java virtual machine. Look here to see how the garbage collected heap fits in with the other parts of the JVM.
  • The Java class file lifestyle -- Gives an overview to the Java class file, the file format into which all Java programs are compiled.
  • Java's garbage-collected heap -- Gives an overview of garbage collection in general and the garbage-collected heap of the JVM in particular.