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

发布于 2024-12-21 14:46:54 字数 1074 浏览 2 评论 0原文

我有这个字节码:

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() 来打印..

I have this byte code:

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]

Update this is the continuation:

> 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 ?

Does the translation is:

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

Is my stack good ?

I am not sure to well understand out().
Probably I will have to use out() setter and to print() after ?

I always use out() to print habitually..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2024-12-28 14:46:54

如果我编译代码

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


对象没有名为 method code>out() 所以我不相信您正在查看您认为是的代码。

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

System.out.println();

这将导致使用 getstatic

If I compile the code

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

and run

javap -c Main

I see

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    

You can see that getstatic loads the field System.out


Object doesn't have a method called out() so I don't believe you are looking at the code you believe you are.

getstatic gets a static fields e.g. System.out is a static field of System so if you write

System.out.println();

This will result in a use of getstatic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文