javap
Bill-Mac:learning Bill$ javap -verbose Test Compiled from "Test.java" public class Test extends java.lang.Object SourceFile: "Test.java" minor version: 0 major version: 50 Constant pool: const #1 = Method #6.#15; // java/lang/Object."<init>":()V const #2 = String #16; // ab1 const #3 = Field #17.#18; // java/lang/System.out:Ljava/io/PrintStream; const #4 = Method #19.#20; // java/io/PrintStream.println:(Ljava/lang/String;)V const #5 = class #21; // Test const #6 = class #22; // java/lang/Object const #7 = Asciz <init>; const #8 = Asciz ()V; const #9 = Asciz Code; const #10 = Asciz LineNumberTable; const #11 = Asciz main; const #12 = Asciz ([Ljava/lang/String;)V; const #13 = Asciz SourceFile; const #14 = Asciz Test.java; const #15 = NameAndType #7:#8;// "<init>":()V const #16 = Asciz ab1; const #17 = class #23; // java/lang/System const #18 = NameAndType #24:#25;// out:Ljava/io/PrintStream; const #19 = class #26; // java/io/PrintStream const #20 = NameAndType #27:#28;// println:(Ljava/lang/String;)V const #21 = Asciz Test; const #22 = Asciz java/lang/Object; const #23 = Asciz java/lang/System; const #24 = Asciz out; const #25 = Asciz Ljava/io/PrintStream;; const #26 = Asciz java/io/PrintStream; const #27 = Asciz println; const #28 = Asciz (Ljava/lang/String;)V; { public Test(); Code: Stack=1, Locals=1, Args_size=1 //所有方法都会有,stack代表栈顶的单位大小(每个大小为1个slot,每个slot是4个字节宽度) 0: aload_0 //第一个0代表虚指令中的行号,每个方法从0开始递增,但是可以跳跃。 1: invokespecial #1; //Method java/lang/Object."<init>":()V //构造方法、父类构造方法以及非静态的private方法调用指令 4: return LineNumberTable: line 2: 0
注:当需要使用一个数据时,它首先会被放入栈顶,使用完后会写回到本地变量或主存中。这里栈的宽度是1,其实代表有一个this将会被引用。
Locals是本地变量的slo个数,但是并不代表与Stack宽度一致。(double、long会占用两个slot)
Args_size代表的是入口参数的个数,不再试slot的个数。
aload_0指令是讲“第一个”slot所在的本地变量推到栈顶,并且这个本地变量是引用类型的。
public static void main(java.lang.String[]); Code: Stack=3, Locals=3, Args_size=1 0: ldc #2; //String ab1 2: astore_1 3: ldc #2; //String ab1 5: astore_2 6: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream; 9: aload_2 10: dup 11: astore_1 12: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 15: return LineNumberTable: line 4: 0 line 5: 3 line 6: 6 line 7: 15 }