iconst n pushes the constant n on the top
of the stackiconst_0 iconst_1 etc. for the
most common constants (-1...5)iload n pushes the contents of local variable
#n iload_0 ... iload_3 iconst 10 iconst 20 iadd // pops 20 and 10, pushes 30
i prefix means “integer”. Similar opcodes
for double (d), long (l), references
(a)iconst 3 ; 3 iconst 4 ; 4 3 imul ; 12 iconst 5 ; 5 12 iadd ; 17
iconst 3 ; 3 iconst 4 ; 4 3 iconst 5 ; 5 4 3 imul ; 20 3 iadd ; 23
iconst 3 ; 3 iconst 4 ; 4 3 iadd ; 7 iconst 5 ; 5 7 imul ; 35
Your turn: http://ActiveLecture.org
iconst 3 ; 3 iconst 7 ; 7 3 invokestatic java/lang/Math.max:(II)I ; 7
/ between components in
javap listing, for historical reasons)<init> for constructor)javap:
I is int, V is
void, etc.)invokestatic (static functions),
invokespecial (constructors, superclass functions)
invokedynamic (everything else)static int sum(int a, int b)
a is local variable 0 (iload_0)b is local variable 1 (iload_1)thisireturn pops off top of stack and returns it
iload_0 iload_1 iadd ireturn

a and
b)
iload_0 iload_1 iadd istore_2 iload_0 iload_1 isub iload_2 imul ireturn
http://ActiveLecture.org

public class Test
{
public static void main(String[] args)
{
int r = 3 + 4 * 5;
}
}
Run it through javap -c. What do you get?
javap output now?public class Test
{
public static void main(String[] args)
{
System.out.println(42);
}
}
What byte codes do you get?
println encoded?
(Hint: Classes are encoded with the quaint
Lname; notation.)println supplied?mystery method:
0:iload_1 1:ifne 8 4:iconst_1 5:goto 17 8:iload_0 9:iload_0 10:iload_1 11:iconst_1 12:isub 13:invokestatic #2; //Method mystery:(II)I 16:imul 17:ireturn
Translate it back into Java. What is your code? (You'll have to look up
ifne in chapter 6 of the JVM spec.)
javap, what
output do you get? Where does it differ from the original?int fac(int n)
{
if (n == 0) return 1;
else return n * fac(n - 1)
}
Translate it into byte codes by hand. What did you get?
javap. What output
do you get? Where does it differ from your hand-assembly?