Java bytecode

This is an old revision of this page, as edited by Psiphiorg (talk | contribs) at 18:42, 23 September 2006 (The Java bytecodes: adding opcodes). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Java bytecode is the form of instructions that the Java virtual machine executes. Each bytecode instruction is one byte in length (hence the name), thus the number of bytecodes is limited to no more than 256. Not all 256 possible bytecode values are used, in fact Sun Microsystems, the original creators of the Java programming language, the Java virtual machine and other components of the Java Runtime Environment, have set aside a number of values to be permanently unimplemented. The bytecodes are given below.

A Java programmer does not need to be aware of or understand Java bytecode at all. However, as suggested in the IBM developerWorks journal, "Understanding bytecode and what bytecode is likely to be generated by a Java compiler helps the Java programmer in the same way that knowledge of assembler helps the C or C++ programmer."[1]

It is possible to write Java bytecode in hand, however this method is never used in real life because nowadays the compilers are able to compile well performing code and no person is able to comprehend a piece of Java bytecode of considerable size. Originally only one compiler existed, the javac compiler from Sun Microsystems, which compiles Java source code to Java bytecode. But because all the specifications for Java bytecode are available other parties have supplied compilers that produce Java bytecode. E.g.:

If executing Java bytecode in a Java virtual machine is not desirable, a developer can also compile Java source code or Java bytecode directly to native machine code with tools such as the GNU Compiler for Java.

Example

Consider the following Java code.

outter: for (int i = 2; i < 1000; i++) {
 for (int j = 2; j < i; j++) {
   if (i % j == 0)
     continue outter;
 }
 System.out.println (i);
}

This is probably translated into byte code like (assuming the above was put in a method):

 Code:
  0:   iconst_2
  1:   istore_1
  2:   iload_1
  3:   sipush  1000
  6:   if_icmpge       44
  9:   iconst_2
  10:  istore_2
  11:  iload_2
  12:  iload_1
  13:  if_icmpge       31
  16:  iload_1
  17:  iload_2
  18:  irem             # remainder
  19:  ifne    25
  22:  goto    38
  25:  iinc    2, 1
  28:  goto    11
  31:  getstatic       #84; //Field java/lang/System.out:Ljava/io/PrintStream;
  34:  iload_1
  35:  invokevirtual   #85; //Method java/io/PrintStream.println:(I)V
  38:  iinc    1, 1
  41:  goto    2
  44:  return

The Java bytecodes

Please see Sun's Java Virtual Machine Specification for more detailed descriptions (link at bottom of page)

Mnemonic Opcode
(in hex)
Description
A
aaload 32 loads onto the stack a reference to an array
aastore 53 stores into a reference to an array
aconst_null 01 pushes a null reference onto the stack
aload 19 loads a reference onto the stack from a local variable
aload_0 2a loads a reference onto the stack from local variable 0
aload_1 2b loads a reference onto the stack from local variable 1
aload_2 2c loads a reference onto the stack from local variable 2
aload_3 2d loads a reference onto the stack from local variable 3
anewarray bd creates a new array of references
areturn b0 returns a reference from a method
arraylength be gets the length of an array
astore 3a stores a reference into a local variable
astore_0 4b stores a reference into local variable 0
astore_1 4c stores a reference into local variable 1
astore_2 4d stores a reference into local variable 2
astore_3 4e stores a reference into local variable 3
athrow bf throws an error or exception
B
baload 33 loads a byte or Boolean value from an array
bastore 54 stores a byte or Boolean value into an array
bipush 10 pushes a byte onto the stack
C
caload 34 loads a char from an array
castore 55 stores a char into an array
checkcast c0 checks whether an object is of a certain type
D
d2f 90 converts a double to a float
d2i 8e converts a double to an int
d2l 8f converts a double to a long
dadd 63 adds two doubles
daload 31 loads a double from an array
dastore 52 stores a double into an array
dcmpg 98 compares two doubles
dcmpl 97 compares two doubles
dconst_0 0e pushes the constant 0.0 onto the stack
dconst_1 0f pushes the constant 1.0 onto the stack
ddiv 6f divides two doubles
dload 18 loads a double from a local variable
dload_0 26 loads a double from local variable 0
dload_1 27 loads a double from local variable 1
dload_2 28 loads a double from local variable 2
dload_3 29 loads a double from local variable 3
dmul 6b multiplies two doubles
dneg 77 negates a double
drem 73 gets the remainder from a division between two doubles
dreturn af returns a double from a method
dstore 39 stores a double into a local variable
dstore_0 47 stores a double into local variable 0
dstore_1 48 stores a double into local variable 1
dstore_2 49 stores a double into local variable 2
dstore_3 4a stores a double into local variable 3
dsub 67 subtracts a double from another
dup 59 duplicates the value on top of the stack
dup_x1 5a inserts a copy of the top value into the stack two values from the top
dup_x2 5b inserts a copy of the top value into the stack two or three values from the top
dup2 5c duplicate top two stack words
dup2_x1 5d duplicate two words and insert beneath third word
dup2_x2 5e duplicate two words and insert beneath fourth word
F
f2d 8d converts a float to a double
f2i 8b converts a float to an int
f2l 8c converts a float to a long
fadd 62 adds two floats
faload 30 loads a float from an array
fastore 51 stores a float in an array
fcmpg 96 compares two floats
fcmpl 95 compares two floats
fconst_0 0b pushes 0.0f on the stack
fconst_1 0c pushes 1.0f on the stack
fconst_2 0d pushes 2.0f on the stack
fdiv 6e divides two floats
fload 17 loads a float from a local value
fload_0 22 loads a float from local variable 0
fload_1 23 loads a float from local variable 1
fload_2 24 loads a float from local variable 2
fload_3 25 loads a float from local variable 3
fmul 6a multiplies two floats
fneg 76 negates a float
frem 72 gets the remainder from a division between two floats
freturn ae returns a float
fstore 38 stores a float into a local variable
fstore_0 43 stores a float into local variable 0
fstore_1 44 stores a float into local variable 1
fstore_2 45 stores a float into local variable 2
fstore_3 46 stores a float into local variable 3
fsub 66 subtracts two floats
G
getfield b4 gets a field of an object
getstatic b2 gets a static field of a class
goto a7 goes to another instruction
goto_w c8 goes to another instruction
I
i2b 91 converts an int into a byte
i2c 92 converts an int into a character
i2d 87 converts an int into a double
i2f 86 converts an int into a float
i2l 85 converts an int into a long
i2s 93 converts an int into a short
iadd 60 adds two ints together
iaload 2e loads an int from an array
iand 7e performs a logical and on two integers
iastore 4f stores an int into an array
iconst_m1 02 loads the int value -1 onto the stack
iconst_0 03 loads the int value 0 onto the stack
iconst_1 04 loads the int value 1 onto the stack
iconst_2 05 loads the int value 2 onto the stack
iconst_3 06 loads the int value 3 onto the stack
iconst_4 07 loads the int value 4 onto the stack
iconst_5 08 loads the int value 5 onto the stack
idiv 6c divides two integers
if_acmpeq a5 branch if reference comparison succeeds
if_acmpne a6 branch if int comparison succeeds
if_icmpeq 9f branch if int is value of comparator
if_icmpne a0 branch if int is not value of comparator
if_icmplt a1 branch if int is less than value of comparator
if_icmpge a2 branch if int is greater than or equal to value of comparator
if_icmpgt a3 branch if int is greater than value of comparator
if_icmple a4 branch if int is less than or equal to value of comparator
ifeq 99 branch if int is 0
ifne 9a branch if int is not 0
iflt 9b branch if int is less than 0
ifge 9c branch if int is greater than or equal to 0
ifgt 9d branch if int is greater than 0
ifle 9e branch if int is less than or equal 0
ifnonnull c7 branch if reference is not null 0
ifnull c6 branch if reference is null 0
iinc 84 increment local variable by constant
iload 15 loads an int from a variable
iload_0 1a loads an int from variable 0
iload_1 1b loads an int from variable 1
iload_2 1c loads an int from variable 2
iload_3 1d loads an int from variable 3
imul 68 multiply two integers
ineg 74 negate int
instanceof c1 determines if an object is of a given type
invokeinterface b9 invokes an interface
invokespecial b7 invoke instance method
invokestatic b8 invoke a static method
invokevirtual b6 invoke virtual method
ior 80 logical int or
irem 70 logical int remainder
ireturn ac returns an integer from a method
ishl 78 int shift left
ishr 7a int shift right
istore 36 store int into variable
istore_0 3b store int into variable 0
istore_1 3c store int into variable 1
istore_2 3d store int into variable 2
istore_3 3e store int into variable 3
isub 64 int subtract
iushr 7c int shift right
ixor 82 int xor
J
jsr a8
jsr_w c9
L
l2d 8a
l2f 89
l2i 88
ladd 61
laload 2f
land 7f
lastore 50
lcmp 94
lconst_0 09 pushes the long value 0.0 onto the stack
lconst_1 0a pushes the long value 0.1 onto the stack
ldc 12
ldc_w 13
ldc2_w 14
ldiv 6d
lload 16
lload_0 1e
lload_1 1f
lload_2 20
lload_3 21
lmul 69
lneg 75
lookupswitch ab
lor 81
lrem 71
lreturn ad
lshl 69
lshr 7b
lstore 37
lstore_0 3f
lstore_1 40
lstore_2 41
lstore_3 42
lsub 65
lushr 7d
lxor 83
M
monitorenter c2
monitorexit c3
multianewarray c5
N
new bb
newarray bc
nop 00 performs no operation
P
pop 57
pop2 58
putfield b5
putstatic b3
R
ret a9
return b1
S
saload 35
sastore 56
sipush
swap 5f
T
tableswitch aa
W
wide c4
Unused
breakpoint ca reserved for breakpoints in Java debuggers; should not appear in any class file
impdep1 fe reserved for implementation-dependent operations within debuggers; should not appear in any class file
impdep2 ff reserved for implementation-dependent operations within debuggers; should not appear in any class file
(no name) cb-fd these values are currently unassigned for opcodes and are reserved for future use
xxxunusedxxx ba this opcode is reserved "for historical reasons"

This list is incomplete.

See also