< Java Programming
Topics:

Java Byte Code is the language to which Java source is compiled and the Java Virtual Machine understands. Unlike compiled languages that have to be specifically compiled for each different type of computers, a Java program only needs to be converted to byte code once, after which it can run on any platform for which a Java Virtual Machine exists.

Bytecode is the compiled format for Java programs. Once a Java program has been converted to bytecode, it can be transferred across a network and executed by Java Virtual Machine (JVM). Bytecode files generally have a .class extension. It is not normally necessary for a Java programmer to know byte code, but it can be useful.

Other Languages

There are a number of exciting new languages being created that also compile to Java byte code, such as Groovy.

GNAT 
The GNU Ada-Compiler, is capable of compiling Ada into Java-style bytecode.
ftp://cs.nyu.edu/pub/gnat
JPython 
Compiles Python to Java-style bytecode.
http://www.jpython.org/
Kawa 
Compiles Scheme to Java-style bytecode.
http://www.gnu.org/software/kawa/


Example

Consider the following Java code.

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

A Java compiler might translate the Java code above into byte code as follows, 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

Example 2

As an example we can write a simple Foo.java source:

public class Foo {
  public static void main(final String[] args) {
    System.out.println("This is a simple example of decompilation using javap");
    a();
    b();
  }
	
  public static void a() {
    System.out.println("Now we are calling a function...");
  }

  public static void b() {
    System.out.println("...and now we are calling b");
  }
}

Compile it and then move Foo.java to another directory or delete it if you wish. What can we do with javap and Foo.class ?

$javap Foo

produces this result:

Compiled from "Foo.java"
public class Foo extends java.lang.Object {
    public Foo();
    public static void main(java.lang.String[]);
    public static void a();
    public static void b();
}

As you can see the javac compiler doesn't strip any (public) variable name from the .class file. As a result the names of the functions, their parameters and types of return are exposed. (This is necessary in order for other classes to access them.)

Let's do a bit more, try:

$javap -c Foo
Compiled from "Foo.java"
public class Foo extends java.lang.Object{
public Foo();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc             #3; //String This is a simple example of decompilation using javap
   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   invokestatic    #5; //Method a:()V
   11:  invokestatic    #6; //Method b:()V
   14:  return

public static void a();
  Code:
   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc             #7; //String Now we are calling a function...
   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   return

public static void b();
  Code:
   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc             #8; //String ...and now we are calling b
   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   return

}

The Java bytecodes

See Oracle's Java Virtual Machine Specification[1] for more detailed descriptions

The manipulation of the operand stack is notated as [before]→[after], where [before] is the stack before the instruction is executed and [after] is the stack after the instruction is executed. A stack with the element 'b' on the top and element 'a' just after the top element is denoted 'a,b'.

MnemonicOpcode
(in hex)
Other bytesStack
[before]→[after]
Description
A
aaload32arrayref, index → valueloads onto the stack a reference from an array
aastore53arrayref, index, value →stores a reference into an array
aconst_null01→ nullpushes a null reference onto the stack
aload19index→ objectrefloads a reference onto the stack from a local variable #index
aload_02a→ objectrefloads a reference onto the stack from local variable 0
aload_12b→ objectrefloads a reference onto the stack from local variable 1
aload_22c→ objectrefloads a reference onto the stack from local variable 2
aload_32d→ objectrefloads a reference onto the stack from local variable 3
anewarraybdindexbyte1, indexbyte2count → arrayrefcreates a new array of references of length count and component type identified by the class reference index (indexbyte1 << 8 + indexbyte2) in the constant pool
areturnb0objectref → [empty]returns a reference from a method
arraylengthbearrayref → lengthgets the length of an array
astore3aindexobjectref →stores a reference into a local variable #index
astore_04bobjectref →stores a reference into local variable 0
astore_14cobjectref →stores a reference into local variable 1
astore_24dobjectref →stores a reference into local variable 2
astore_34eobjectref →stores a reference into local variable 3
athrowbfobjectref → [empty], objectrefthrows an error or exception (notice that the rest of the stack is cleared, leaving only a reference to the Throwable)
B
baload33arrayref, index → valueloads a byte or Boolean value from an array
bastore54arrayref, index, value →stores a byte or Boolean value into an array
bipush10byte→ valuepushes a byte onto the stack as an integer value
C
caload34arrayref, index → valueloads a char from an array
castore55arrayref, index, value →stores a char into an array
checkcastc0indexbyte1, indexbyte2objectref → objectrefchecks whether an objectref is of a certain type, the class reference of which is in the constant pool at index (indexbyte1 << 8 + indexbyte2)
D
d2f90value → resultconverts a double to a float
d2i8evalue → resultconverts a double to an int
d2l8fvalue → resultconverts a double to a long
dadd63value1, value2 → resultadds two doubles
daload31arrayref, index → valueloads a double from an array
dastore52arrayref, index, value →stores a double into an array
dcmpg98value1, value2 → resultcompares two doubles
dcmpl97value1, value2 → resultcompares two doubles
dconst_00e→ 0.0pushes the constant 0.0 onto the stack
dconst_10f→ 1.0pushes the constant 1.0 onto the stack
ddiv6fvalue1, value2 → resultdivides two doubles
dload18index→ valueloads a double value from a local variable #index
dload_026→ valueloads a double from local variable 0
dload_127→ valueloads a double from local variable 1
dload_228→ valueloads a double from local variable 2
dload_329→ valueloads a double from local variable 3
dmul6bvalue1, value2 → resultmultiplies two doubles
dneg77value → resultnegates a double
drem73value1, value2 → resultgets the remainder from a division between two doubles
dreturnafvalue → [empty]returns a double from a method
dstore39indexvalue →stores a double value into a local variable #index
dstore_047value →stores a double into local variable 0
dstore_148value →stores a double into local variable 1
dstore_249value →stores a double into local variable 2
dstore_34avalue →stores a double into local variable 3
dsub67value1, value2 → resultsubtracts a double from another
dup59value → value, valueduplicates the value on top of the stack
dup_x15avalue2, value1 → value1, value2, value1inserts a copy of the top value into the stack two values from the top
dup_x25bvalue3, value2, value1 → value1, value3, value2, value1inserts a copy of the top value into the stack two (if value2 is double or long it takes up the entry of value3, too) or three values (if value2 is neither double nor long) from the top
dup25c{value2, value1} → {value2, value1}, {value2, value1}duplicate top two stack words (two values, if value1 is not double nor long; a single value, if value1 is double or long)
dup2_x15dvalue3, {value2, value1} → {value2, value1}, value3, {value2, value1}duplicate two words and insert beneath third word (see explanation above)
dup2_x25e{value4, value3}, {value2, value1} → {value2, value1}, {value4, value3}, {value2, value1}duplicate two words and insert beneath fourth word
F
f2d8dvalue → resultconverts a float to a double
f2i8bvalue → resultconverts a float to an int
f2l8cvalue → resultconverts a float to a long
fadd62value1, value2 → resultadds two floats
faload30arrayref, index → valueloads a float from an array
fastore51arreyref, index, value →stores a float in an array
fcmpg96value1, value2 → resultcompares two floats
fcmpl95value1, value2 → resultcompares two floats
fconst_00b→ 0.0fpushes 0.0f on the stack
fconst_10c→ 1.0fpushes 1.0f on the stack
fconst_20d→ 2.0fpushes 2.0f on the stack
fdiv6evalue1, value2 → resultdivides two floats
fload17index→ valueloads a float value from a local variable #index
fload_022→ valueloads a float value from local variable 0
fload_123→ valueloads a float value from local variable 1
fload_224→ valueloads a float value from local variable 2
fload_325→ valueloads a float value from local variable 3
fmul6avalue1, value2 → resultmultiplies two floats
fneg76value → resultnegates a float
frem72value1, value2 → resultgets the remainder from a division between two floats
freturnaevalue → [empty]returns a float from method
fstore38indexvalue →stores a float value into a local variable #index
fstore_043value →stores a float value into local variable 0
fstore_144value →stores a float value into local variable 1
fstore_245value →stores a float value into local variable 2
fstore_346value →stores a float value into local variable 3
fsub66value1, value2 → resultsubtracts two floats
G
getfieldb4index1, index2objectref → valuegets a field value of an object objectref, where the field is identified by field reference in the constant pool index (index1 << 8 + index2)
getstaticb2index1, index2→ valuegets a static field value of a class, where the field is identified by field reference in the constant pool index (index1 << 8 + index2)
gotoa7branchbyte1, branchbyte2[no change]goes to another instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
goto_wc8branchbyte1, branchbyte2, branchbyte3, branchbyte4[no change]goes to another instruction at branchoffset (signed int constructed from unsigned bytes branchbyte1 << 24 + branchbyte2 << 16 + branchbyte3 << 8 + branchbyte4)
I
i2b91value → resultconverts an int into a byte
i2c92value → resultconverts an int into a character
i2d87value → resultconverts an int into a double
i2f86value → resultconverts an int into a float
i2l85value → resultconverts an int into a long
i2s93value → resultconverts an int into a short
iadd60value1, value2 → resultadds two ints together
iaload2earrayref, index → valueloads an int from an array
iand7evalue1, value2 → resultperforms a logical and on two integers
iastore4farrayref, index, value →stores an int into an array
iconst_m102→ -1loads the int value -1 onto the stack
iconst_003→ 0loads the int value 0 onto the stack
iconst_104→ 1loads the int value 1 onto the stack
iconst_205→ 2loads the int value 2 onto the stack
iconst_306→ 3loads the int value 3 onto the stack
iconst_407→ 4loads the int value 4 onto the stack
iconst_508→ 5loads the int value 5 onto the stack
idiv6cvalue1, value2 → resultdivides two integers
if_acmpeqa5branchbyte1, branchbyte2value1, value2 →if references are equal, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
if_acmpnea6branchbyte1, branchbyte2value1, value2 →if references are not equal, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
if_icmpeq9fbranchbyte1, branchbyte2value1, value2 →if ints are equal, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
if_icmpnea0branchbyte1, branchbyte2value1, value2 →if ints are not equal, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
if_icmplta1branchbyte1, branchbyte2value1, value2 →if value1 is less than value2, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
if_icmpgea2branchbyte1, branchbyte2value1, value2 →if value1 is greater than or equal to value2, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
if_icmpgta3branchbyte1, branchbyte2value1, value2 →if value1 is greater than value2, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
if_icmplea4branchbyte1, branchbyte2value1, value2 →if value1 is less than or equal to value2, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
ifeq99branchbyte1, branchbyte2value →if value is 0, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
ifne9abranchbyte1, branchbyte2value →if value is not 0, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
iflt9bbranchbyte1, branchbyte2value →if value is less than 0, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
ifge9cbranchbyte1, branchbyte2value →if value is greater than or equal to 0, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
ifgt9dbranchbyte1, branchbyte2value →if value is greater than 0, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
ifle9ebranchbyte1, branchbyte2value →if value is less than or equal to 0, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
ifnonnullc7branchbyte1, branchbyte2value →if value is not null, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
ifnullc6branchbyte1, branchbyte2value →if value is null, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)
iinc84index, const[No change]increment local variable #index by signed byte const
iload15index→ valueloads an int value from a variable #index
iload_01a→ valueloads an int value from variable 0
iload_11b→ valueloads an int value from variable 1
iload_21c→ valueloads an int value from variable 2
iload_31d→ valueloads an int value from variable 3
imul68value1, value2 → resultmultiply two integers
ineg74value → resultnegate int
instanceofc1indexbyte1, indexbyte2objectref → resultdetermines if an object objectref is of a given type, identified by class reference index in constant pool (indexbyte1 << 8 + indexbyte2)
invokeinterfaceb9indexbyte1, indexbyte2, count, 0objectref, [arg1, arg2, ...] →invokes an interface method on object objectref, where the interface method is identified by method reference index in constant pool (indexbyte1 << 8 + indexbyte2) and count is the number of arguments to pop from the stack frame including the object on which the method is being called and must always be greater than or equal to 1
invokespecialb7indexbyte1, indexbyte2objectref, [arg1, arg2, ...] →invoke instance method on object objectref requiring special handling (instance initialization method, a private method, or a superclass method), where the method is identified by method reference index in constant pool (indexbyte1 << 8 + indexbyte2)
invokestaticb8indexbyte1, indexbyte2[arg1, arg2, ...] →invoke a static method, where the method is identified by method reference index in constant pool (indexbyte1 << 8 + indexbyte2)
invokevirtualb6indexbyte1, indexbyte2objectref, [arg1, arg2, ...] →invoke virtual method on object objectref, where the method is identified by method reference index in constant pool (indexbyte1 << 8 + indexbyte2)
ior80value1, value2 → resultlogical int or
irem70value1, value2 → resultlogical int remainder
ireturnacvalue → [empty]returns an integer from a method
ishl78value1, value2 → resultint shift left
ishr7avalue1, value2 → resultint shift right
istore36indexvalue →store int value into variable #index
istore_03bvalue →store int value into variable 0
istore_13cvalue →store int value into variable 1
istore_23dvalue →store int value into variable 2
istore_33evalue →store int value into variable 3
isub64value1, value2 → resultint subtract
iushr7cvalue1, value2 → resultint shift right
ixor82value1, value2 → resultint xor
J
jsra8branchbyte1, branchbyte2→ addressjump to subroutine at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2) and place the return address on the stack
jsr_wc9branchbyte1, branchbyte2, branchbyte3, branchbyte4→ addressjump to subroutine at branchoffset (signed int constructed from unsigned bytes branchbyte1 << 24 + branchbyte2 << 16 + branchbyte3 << 8 + branchbyte4) and place the return address on the stack
L
l2d8avalue → resultconverts a long to a double
l2f89value → resultconverts a long to a float
l2i88value → resultconverts a long to an int
ladd61value1, value2 → resultadd two longs
laload2farrayref, index → valueload a long from an array
land7fvalue1, value2 → resultbitwise and of two longs
lastore50arrayref, index, value →store a long to an array
lcmp94value1, value2 → resultcompares two longs values
lconst_009→ 0Lpushes the long 0 onto the stack
lconst_10a→ 1Lpushes the long 1 onto the stack
ldc12index→ valuepushes a constant #index from a constant pool (String, int, float or class type) onto the stack
ldc_w13indexbyte1, indexbyte2→ valuepushes a constant #index from a constant pool (String, int, float or class type) onto the stack (wide index is constructed as indexbyte1 << 8 + indexbyte2)
ldc2_w14indexbyte1, indexbyte2→ valuepushes a constant #index from a constant pool (double or long) onto the stack (wide index is constructed as indexbyte1 << 8 + indexbyte2)
ldiv6dvalue1, value2 → resultdivide two longs
lload16index→ valueload a long value from a local variable #index
lload_01e→ valueload a long value from a local variable 0
lload_11f→ valueload a long value from a local variable 1
lload_220→ valueload a long value from a local variable 2
lload_321→ valueload a long value from a local variable 3
lmul69value1, value2 → resultmultiplies two longs
lneg75value → resultnegates a long
lookupswitchab<0-3 bytes padding>, defaultbyte1, defaultbyte2, defaultbyte3, defaultbyte4, npairs1, npairs2, npairs3, npairs4, match-offset pairs...key →a target address is looked up from a table using a key and execution continues from the instruction at that address
lor81value1, value2 → resultbitwise or of two longs
lrem71value1, value2 → resultremainder of division of two longs
lreturnadvalue → [empty]returns a long value
lshl79value1, value2 → resultbitwise shift left of a long value1 by value2 positions
lshr7bvalue1, value2 → resultbitwise shift right of a long value1 by value2 positions
lstore37indexvalue →store a long value in a local variable #index
lstore_03fvalue →store a long value in a local variable 0
lstore_140value →store a long value in a local variable 1
lstore_241value →store a long value in a local variable 2
lstore_342value →store a long value in a local variable 3
lsub65value1, value2 → resultsubtract two longs
lushr7dvalue1, value2 → resultbitwise shift right of a long value1 by value2 positions, unsigned
lxor83value1, value2 → resultbitwise exclusive or of two longs
M
monitorenterc2objectref →enter monitor for object ("grab the lock" - start of synchronized() section)
monitorexitc3objectref →exit monitor for object ("release the lock" - end of synchronized() section)
multianewarrayc5indexbyte1, indexbyte2, dimensionscount1, [count2,...] → arrayrefcreate a new array of dimensions dimensions with elements of type identified by class reference in constant pool index (indexbyte1 << 8 + indexbyte2); the sizes of each dimension is identified by count1, [count2, etc]
N
newbbindexbyte1, indexbyte2→ objectrefcreates new object of type identified by class reference in constant pool index (indexbyte1 << 8 + indexbyte2)
newarraybcatypecount → arrayrefcreates new array with count elements of primitive type identified by atype
nop00[No change]performs no operation
P
pop57value →discards the top value on the stack
pop258{value2, value1} →discards the top two values on the stack (or one value, if it is a double or long)
putfieldb5indexbyte1, indexbyte2objectref, value →set field to value in an object objectref, where the field is identified by a field reference index in constant pool (indexbyte1 << 8 + indexbyte2)
putstaticb3indexbyte1, indexbyte2value →set static field to value in a class, where the field is identified by a field reference index in constant pool (indexbyte1 << 8 + indexbyte2)
R
reta9index[No change]continue execution from address taken from a local variable #index (the asymmetry with jsr is intentional)
returnb1→ [empty]return void from method
S
saload35arrayref, index → valueload short from array
sastore56arrayref, index, value →store short to array
sipush11byte1, byte2→ valuepushes a signed integer (byte1 << 8 + byte2) onto the stack
swap5fvalue2, value1 → value1, value2swaps two top words on the stack (note that value1 and value2 must not be double or long)
T
tableswitchaa[0-3 bytes padding], defaultbyte1, defaultbyte2, defaultbyte3, defaultbyte4, lowbyte1, lowbyte2, lowbyte3, lowbyte4, highbyte1, highbyte2, highbyte3, highbyte4, jump offsets...index →continue execution from an address in the table at offset index
W
widec4opcode, indexbyte1, indexbyte2
or
iinc, indexbyte1, indexbyte2, countbyte1, countbyte2
[same as for corresponding instructions]execute opcode, where opcode is either iload, fload, aload, lload, dload, istore, fstore, astore, lstore, dstore, or ret, but assume the index is 16 bit; or execute iinc, where the index is 16 bits and the constant to increment by is a signed 16 bit short
Unused
breakpointcareserved for breakpoints in Java debuggers; should not appear in any class file
impdep1fereserved for implementation-dependent operations within debuggers; should not appear in any class file
impdep2ffreserved for implementation-dependent operations within debuggers; should not appear in any class file
(no name)cb-fdthese values are currently unassigned for opcodes and are reserved for future use
xxxunusedxxxbathis opcode is reserved "for historical reasons"

References

  1. Oracle's Java Virtual Machine Specification
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.