简体   繁体   English

java中的局部变量访问

[英]Local Variable access in java

Java uses stack to store local variable. Java 使用栈来存储局部变量。

看局部变量栈的图片

So, how java access local variables.那么,java如何访问局部变量。 Suppose I want to access the variable 'b', will it first pop the 'd' and 'c' variable to access it?假设我想访问变量 'b',它会先弹出 'd' 和 'c' 变量来访问它吗? If not how it is storing in stack and access when a programmer have to access local variable?如果不是,当程序员必须访问局部变量时,它如何存储在堆栈中并访问?

public class Solution {  
 public static void main(String args[]){  
    int a = 5;
    int b = 8;
    int c = 3;
    int d = c*3;
    System.out.println("Testing c "+c);
    System.out.println("Testing b "+b);
    System.out.println("Testing d "+d);
 }
}

Java uses stack to store local variable. Java 使用栈来存储局部变量。

This is more or less incorrect.这或多或少是不正确的。

Java in bytecode form is a combination of stacks and accessible variables.字节码形式的 Java 是堆栈和可访问变量的组合。 Yes, there is a stack, but there is also a small variable storage system, and most local variables end up in this system instead;是的,有一个栈,但也有一个小的变量存储系统,大多数局部变量最终都在这个系统中; bytecode can be generated to put a variable in a slot, and load a variable from a slot, without having to continuously push and pop all things 'on top' first.可以生成字节码以将变量放入插槽中,并从插槽加载变量,而不必先连续推入和弹出“顶部”的所有内容。

Here's a trivial example;这是一个简单的例子; take java code:取java代码:

public static void test() {
    Test t = new Test();
    System.out.println(t.a + t.b);
}

class Test {
    int a, b;
}

turns into this bytecode:变成这个字节码:

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).

notes on the above:上述注意事项:

[1] in bytecode, the largest the stack will ever be, and the most of those 'local vars' we ever need is tracked. [1] 在字节码中,堆栈将是最大的,并且我们需要的那些“本地变量”中的大部分都被跟踪。 You can see this with javap.你可以用 javap 看到这一点。

[2] There is a constant pool. [2] 有一个常量池。 The 'index' into the constant pool containing, for example, a full method reference (so fully qualified class name + method name + method's signature) is part of the bytecode for things like 'invoke this constructor', 'read this field', etc. The #x marks in javap output are references to this constant pool.例如,包含完整方法引用的常量池中的“索引”(即完全限定的类名 + 方法名 + 方法的签名)是字节码的一部分,用于诸如“调用此构造函数”、“读取此字段”、等等。javap 输出中的#x 标记是对这个常量池的引用。

For your snippet, I invite you to do the same thing I did: Write it up in a really simple java file, compile it, then run javap -c -private MyClass and have a look!对于你的代码片段,我邀请你做和我一样的事情:把它写在一个非常简单的 java 文件中,编译它,然后运行javap -c -private MyClass看看!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM