gpt4 book ai didi

java - getstatic 在字节码中的真正含义是什么?

转载 作者:搜寻专家 更新时间:2023-10-31 20:22:46 24 4
gpt4 key购买 nike

我有这个字节码:

new                 java.lang.Object
// stack is [newObjectRef]
dup
// Stack is [newObjectRef newObjectRef]
invokespecial void java.lang.Object.<init>()
// Stack is [initializedAsTypeObjectObjectRef]
putstatic java.lang.Object class.a
// variable a has the reference of new object
getstatic java.io.PrintStream java.lang.System.out
// Take the static value of System.out
// Stack is [initializedAsTypeObjectObjectRef System.out]

更新这是延续:

> ldc                 "test" // Stack is
> [initializedAsTypeObjectObjectRef System.out "test"]
> jsr pos.0000026C // call a subrutine invokevirtual void
> java.io.PrintStream.println(java.lang.String) // actually print the
> result // stack is (I think) Empty at this time ?

请问翻译是:

  Object a = new Object();
a = "test";
System.out.print(a);

我的堆栈好吗?

我不确定是否能很好地理解 out()。可能我将不得不使用 out() setter 并在之后使用 print() ?

我总是习惯性地使用out()打印..

最佳答案

如果我编译代码

public static void main(String[] args) {
Object a;
a = "test";
System.out.println(a);
}

然后运行

javap -c Main

我明白了

public static void main(java.lang.String[]);
Code:
0: ldc #2 // String test
2: astore_1
3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
6: aload_1
7: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
10: return

可以看到getstatic加载字段System.out


对象没有名为 out()方法,所以我不相信您正在查看您认为自己是的代码。

getstatic 获取一个 static 字段,例如System.out 是 System 的一个static 字段,所以如果你写

System.out.println();

这将导致使用 getstatic

关于java - getstatic 在字节码中的真正含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8546124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com