JVM堆栈中的本地变量表



今天我把一个类编译成字节码,但在主方法局部变量表索引0中找不到任何东西,它是从1开始的,我知道一个非静态方法从1开始,因为它在0处是"this"。

这是字节码。

// this is the main method
public static void main(java.lang.String[]);
Code:
0: iconst_1
1: istore_1   // index: 1 (what is content at index 0 ?)
2: iconst_2
3: istore_2
4: return
// this is a static method
public static void staticMethod();
Code:
0: iconst_1
1: istore_0  // index: 0 (no 'this')
2: iconst_2
3: istore_1
4: return
// this is a non-static method
public void nonStaticMethod();
Code:
0: iconst_1
1: istore_1  // index: 1  (index 0 should be 'this')
2: iconst_1
3: istore_2
4: return

请帮帮我,谢谢!

main方法中索引0处的局部变量是该方法的参数:对String[]的引用。来自JVM规范:

Java虚拟机在方法调用时使用局部变量传递参数。在类方法调用中,从局部变量0开始,在连续的局部变量中传递任何参数。

https://docs.oracle.com/javase/specs/jvms/se14/html/jvms-2.html#jvms-2.6.1

最新更新