Java 使用栈来存储局部变量。
这或多或少是不正确的。
字节码形式的 Java 是堆栈和可访问变量的组合。是的,有一个栈,但也有一个小的变量存储系统,大多数局部变量最终都在这个系统中;可以生成字节码以将变量放入槽中,并从槽中加载变量,而不必先连续推送和弹出所有“在顶部”的东西。
这是一个简单的例子;取java代码:
public static void test() {
Test t = new Test();
System.out.println(t.a + t.b);
}
class Test {
int a, b;
}
变成这个字节码:
meta: stack=3, locals=2 ; [1]
INVOKESPECIAL #9 ; [2] 'new Test() -> result on stack'
stack now: instance of Test
ASTORE_1 ; Pop stack and put result in 'slot 1'
stack now: empty.
GETSTATIC #10 ; Read field 'System.out' -> result on stack.
stack now: Sysout
ALOAD_1 ; Copy 'slot 1' onto stack.
stack now: instance of Test, Sysout.
GETFIELD #16 ; Consume top of stack, read a field, put that on stack.
stack now: int (t.a), Sysout.
ALOAD_1
stack now: instance of Test, t.a, Sysout.
GETFIELD #20
stack now: t.b, t.a, Sysout.
IADD ; this has no params, it's all stack based.
stack now: the sum of t.a+t.b, Sysout.
INVOKEVIRTUAL #23 ; the println method
stack now: empty (the invokevirtual consumed it all).
以上注意事项:
[1] 在字节码中,堆栈将是最大的,并且我们需要跟踪的大多数“本地变量”。你可以用 javap 看到这个。
[2] 有一个常量池。常量池中的“索引”包含例如完整的方法引用(完全限定的类名 + 方法名 + 方法的签名)是字节码的一部分,用于“调用此构造函数”、“读取此字段”、等等。javap 输出中的#x 标记是对该常量池的引用。
为了你的 sn-p,我邀请你做和我一样的事情:把它写在一个非常简单的 java 文件中,编译它,然后运行javap -c -private MyClass 看看!