第一步,先引入一段简单的java代码。如下代码:
public class Math {
public static final Integer COSTANT = 66;
public int math(){
int a = 1;
int b = 2;
int c = (a + b) * 10;
return c;
}
public static void main(String[] args) {
Math m = new Math ();
m.math ();
}
}
第二步,用javap -c Math.class 把Math.class类编译成java的字节码。
public class com.boye.test.exam.Math {
public static final java.lang.Integer COSTANT;
public com.boye.test.exam.Math();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public int math();
Code:
0: iconst_1
1: istore_1
2: iconst_2
3: istore_2
4: iload_1
5: iload_2
6: iadd
7: bipush 10
9: imul
10: istore_3
11: iload_3
12: ireturn
public static void main(java.lang.String[]);
Code:
0: new #2 // class com/boye/test/exam/Math
3: dup
4: invokespecial #3 // Method "<init>":()V
7: astore_1
8: aload_1
9: invokevirtual #4 // Method math:()I
12: pop
13: return
static {};
Code:
0: bipush 66
2: invokestatic #5 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: putstatic #6 // Field COSTANT:Ljava/lang/Integer;
8: return
}
编译后的字节码对于java的初学者或者对java的虚拟机原理未曾理解过的人,依然是很生涩难懂的。不过好在有专门的java字节码查询途径。
最后,绘出Math.class的JVM原理图:
