28. javap
• Java class file disassembler
• Used with no options shows class structure
only
– Methods, superclass, interfaces, etc
• -c shows the bytecode
• -private shows all methods and members
• -s prints internal signatures
• -l prints line numbers and local variable
tables
32. C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
33. C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
Code: the default constructor
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
34. C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
Code: push this to stack
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
35. C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
invoke <init> on this
36. C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
37. C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
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 Hello, World!
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
38. C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
get static field
public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String Hello, World!
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
39. C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
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 Hello, World!
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
load string to the stack
40. C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
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 Hello, World!
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
invoke method with parameter
41. C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
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 Hello, World!
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
42. What’s #1,#2, etc ?
C:workgeeconclasses>javap Hello -c
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
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 Hello, World!
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
55. Stack Machine
• JVM is a stack-based machine
• Each thread has a stack
• Stack stores frames
56. Stack Machine
• JVM is a stack-based machine
• Each thread has a stack
• Stack stores frames
• Frame is created on method invocation
57. Stack Machine
• JVM is a stack-based machine
• Each thread has a stack
• Stack stores frames
• Frame is created on method invocation
• Frame consists of:
– Operand stack
– Array of local variables
83. Local Variables
public int calculate(int);
Code:
Stack=2, Locals=2, Args_size=2
…
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this LLocalVariables;
0 5 1 value I
84. Local Variables
public int calculate(int);
Code:
Stack=2, Locals=2, Args_size=2
…
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this LLocalVariables;
0 5 1 value I
85. Local Variables
public int calculate(int);
Code:
Stack=2, Locals=2, Args_size=2
…
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this LLocalVariables;
0 5 1 value I
86. Local Variables
The table
public int calculate(int);
Code: maps
Stack=2, Locals=2, Args_size=2 numbers to
…
names
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this LLocalVariables;
0 5 1 value I
87. Local Variables
public int calculate(int);
Code:
Stack=2, Locals=2, Args_size=2 Sized explicitly
…
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this LLocalVariables;
0 5 1 value I
88. Local Variables Stack
var value depth value
ldc "Hello"
0 0
astore_0
1 iconst_1 1
astore_1
2 aload_0 2
3 3
4 4
89. Local Variables Stack
var value depth value
ldc "Hello"
0 0 "Hello"
astore_0
1 iconst_1 1
astore_1
2 aload_0 2
3 3
4 4
90. Local Variables Stack
var value depth value
ldc "Hello"
0 "Hello" 0
astore_0
1 iconst_1 1
astore_1
2 aload_0 2
3 3
4 4
91. Local Variables Stack
var value depth value
ldc "Hello"
0 "Hello" 0 1
astore_0
1 iconst_1 1
astore_1
2 aload_0 2
3 3
4 4
92. Local Variables Stack
var value depth value
ldc "Hello"
0 "Hello" 0
astore_0
1 1 iconst_1 1
astore_1
2 aload_0 2
3 3
4 4
93. Local Variables Stack
var value depth value
ldc "Hello"
0 "Hello" 0 "Hello"
astore_0
1 1 iconst_1 1
astore_1
2 aload_0 2
3 3
4 4
118. invokevirtual
class A
• invokestatic A/method1
A/method2
• invokespecial
• invokevirtual
• invokeinterface
• invokedynamic
119. invokevirtual
class A
• invokestatic A/method1
A/method2
• invokespecial
class B
• invokevirtual
• invokeinterface
• invokedynamic
120. invokevirtual
class A
• invokestatic A/method1
A/method2
• invokespecial
class B
• invokevirtual A/method1
B/method2
• invokeinterface B/method3
• invokedynamic
121. invokeinterface
class A
• invokestatic A/method1
A/method2
• invokespecial
class B impl X
• invokevirtual A/method1
B/method2
• invokeinterface B/method3
X/methodX
• invokedynamic
122. invokeinterface
class A
• invokestatic A/method1
A/method2
• invokespecial
class B impl X
• invokevirtual A/method1
• invokeinterface
B/method2
B/method3
D impl X
X/methodX D/method1
• invokedynamic X/methodX
123. invokeinterface
class A
• invokestatic A/method1
A/method2
• invokespecial
class B impl X
• invokevirtual A/method1
• invokeinterface
B/method2
B/method3
D impl X
X/methodX D/method1
• invokedynamic X/methodX
124. invokeinterface
class A
• invokestatic A/method1
A/method2
• invokespecial
class B impl X
• invokevirtual A/method1
• invokeinterface
B/method2
B/method3
D impl X
X/methodX D/method1
• invokedynamic X/methodX
Efficient Implementation of Java Interfaces: Invokeinterface Considered
Harmless, Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and
Derek Lieber, OOPSLA’01
143. public final class Singleton extends java.lang.Object {
public static final void test();
Code:
0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;
3: invokevirtual #13; //Method Singleton$.test:()V
6: return
}
144. public final class Singleton extends java.lang.Object {
public static final void test();
Code:
0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;
3: invokevirtual #13; //Method Singleton$.test:()V
6: return
}
145. public final class Singleton extends java.lang.Object {
public static final void test();
Code:
0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;
3: invokevirtual #13; //Method Singleton$.test:()V
6: return
}
146. public final class Singleton extends java.lang.Object {
public static final void test();
Code:
0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;
3: invokevirtual #13; //Method Singleton$.test:()V
6: return
}
public final class Singleton$ extends java.lang.Object implements
scala.ScalaObject {
public static final Singleton$ MODULE$;
public static {};
Code:
0: new #9; //class Singleton$
3: invokespecial #12; //Method "<init>":()V
6: return
public void test();
private Singleton$();
}
147. public final class Singleton extends java.lang.Object {
public static final void test();
Code:
0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;
3: invokevirtual #13; //Method Singleton$.test:()V
6: return
}
public final class Singleton$ extends java.lang.Object implements
scala.ScalaObject {
public static final Singleton$ MODULE$;
public static {};
Code:
0: new #9; //class Singleton$
3: invokespecial #12; //Method "<init>":()V
6: return
public void test();
private Singleton$();
}
148. public final class Singleton extends java.lang.Object {
public static final void test();
Code:
0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;
3: invokevirtual #13; //Method Singleton$.test:()V
6: return
}
public final class Singleton$ extends java.lang.Object implements
scala.ScalaObject {
public static final Singleton$ MODULE$;
public static {};
Code:
0: new #9; //class Singleton$
3: invokespecial #12; //Method "<init>":()V
6: return
public void test();
private Singleton$();
}
149. public final class Singleton extends java.lang.Object {
public static final void test();
Code:
0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$;
3: invokevirtual #13; //Method Singleton$.test:()V
6: return
}
public final class Singleton$ extends java.lang.Object implements
scala.ScalaObject {
public static final Singleton$ MODULE$;
public static {};
public void test();
private Singleton$();
Code:
0: aload_0
1: invokespecial #17; //Method java/lang/Object."<init>":()V
4: aload_0
5: putstatic #19; //Field MODULE$:LSingleton$;
8: return
151. public class Singleton {
public void test(){
Singleton$.MODULE$.test();
}
}
object Singleton {
def test={}
}
152. public class Singleton {
public void test(){
Singleton$.MODULE$.test();
}
}
public final class Singleton$
object Singleton { implements scala.ScalaObject {
def test={} public static final Singleton$ MODULE$;
} static { new Singleton$(); }
private Singleton$(){
MODULE$ = this;
}
public void test() {
}
}