gpt4 book ai didi

java - javap命令的输出

转载 作者:行者123 更新时间:2023-12-01 17:17:58 25 4
gpt4 key购买 nike

我使用 javap 检查了我的类文件命令,我无法理解下面的部分

Classfile /D:/WaitNotifyExample.class

Last modified Dec 20, 2013; size 622 bytes
MD5 checksum 4781f8cf8062fa75efa30c76adc25cfb
Compiled from "WaitNotifyExample.java"
public class WaitNotifyExample
SourceFile: "WaitNotifyExample.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER

Constant pool:
#1 = Methodref #12.#24 // java/lang/Object."<init>":()V
#2 = Class #25 // java/lang/String
#3 = String #26 // I am hidden
#4 = Methodref #2.#27 // java/lang/String."<init>":
(Ljava/lang/String;)V
#5 = Fieldref #11.#28 // WaitNotifyExample.s1:Ljava/lang/String;
#6 = String #29 // I am diving into Pool
#7 = Fieldref #11.#30 // WaitNotifyExample.s2:Ljava/lang/String;
#8 = Fieldref #31.#32 // java/lang/System.out:Ljava/io/PrintStream;
#9 = String #33 // hello World
#10 = Methodref #34.#35 // java/io/PrintStream.println:
(Ljava/lang/String;)V
#11 = Class #36 // WaitNotifyExample
#12 = Class #37 // java/lang/Object
#13 = Utf8 s1
#14 = Utf8 Ljava/lang/String;
#15 = Utf8 s2
#16 = Utf8 <init>
#17 = Utf8 ()V
#18 = Utf8 Code
#19 = Utf8 LineNumberTable
#20 = Utf8 main
#21 = Utf8 ([Ljava/lang/String;)V
#22 = Utf8 SourceFile
#23 = Utf8 WaitNotifyExample.java
#24 = NameAndType #16:#17 // "<init>":()V
#25 = Utf8 java/lang/String
#26 = Utf8 I am hidden
#27 = NameAndType #16:#38 // "<init>":(Ljava/lang/String;)V
#28 = NameAndType #13:#14 // s1:Ljava/lang/String;
#29 = Utf8 I am diving into Pool
#30 = NameAndType #15:#14 // s2:Ljava/lang/String;
#31 = Class #39 // java/lang/System
#32 = NameAndType #40:#41 // out:Ljava/io/PrintStream;
#33 = Utf8 hello World
#34 = Class #42 // java/io/PrintStream
#35 = NameAndType #43:#38 // println:(Ljava/lang/String;)V
#36 = Utf8 WaitNotifyExample
#37 = Utf8 java/lang/Object
#38 = Utf8 (Ljava/lang/String;)V
#39 = Utf8 java/lang/System
#40 = Utf8 out
#41 = Utf8 Ljava/io/PrintStream;
#42 = Utf8 java/io/PrintStream
#43 = Utf8 println

{

public java.lang.String s1;
flags: ACC_PUBLIC


public java.lang.String s2;
flags: ACC_PUBLIC


public WaitNotifyExample();
flags: ACC_PUBLIC

Code:
stack=4, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: new #2 // class java/lang/String
8: dup
9: ldc #3 // String I am hidden
11: invokespecial #4 // Method java/lang/String."<init>":(Ljava/lang/String;)V
14: putfield #5 // Field s1:Ljava/lang/String;
17: aload_0
18: new #2 // class java/lang/String
21: dup
22: ldc #6 // String I am diving into Pool
24: invokespecial #4 // Method java/lang/String."<init>":(Ljava/lang/String;)V
27: putfield #7 // Field s2:Ljava/lang/String;
30: return
LineNumberTable:
line 2: 0
line 4: 4
line 5: 17

public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC

Code:
stack=2, locals=2, args_size=1
0: iconst_0
1: istore_1
2: getstatic #8 // Field java/lang/System.out:Ljava/io/PrintStream;
5: ldc #9 // String hello World
7: invokevirtual #10 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
10: return
LineNumberTable:
line 7: 0
line 8: 2
line 9: 10
}

编辑:下面是相应的java代码。

 public class WaitNotifyExample {

public String s1 = new String("I am hidden");
public String s2 = new String("I am diving into Pool");

public static void main(String[] args) {
int mainLiteral = 0;
System.out.println("hello World");
}

}

我不明白上述类文件中的以下内容:

  1. 堆栈=4、局部变量=1、args_size=1
  2. 行号表:部分

最佳答案

stack=4, locals=1, args_size=1

字节码中的值存储在局部变量槽和操作数堆栈中。每个插槽最多可以有 2^16 - 1 个插槽,但出于效率原因,您需要指定堆栈和局部变量的限制,以便在您实际不使用它时不会浪费所有空间。

在编译的代码中,编译器会自动将其计算为函数中实际使用的最小值。在本例中,它使用操作数堆栈中的 4 个槽和局部变量表中的 1 个槽。我不确定 arg_size 是什么,但我猜测这只是参数的总大小(即参数的数量)。无论如何,这与类文件格式的任何功能都不对应,因此无论它是什么,javap都会手动计算并插入它。

LineNumberTable: section

LineNumberTable 是保存用于调试目的的元数据的可选属性之一。在这种情况下,它指定字节码中的哪些偏移量对应于原始源代码中的每一行。这对于打印更多信息堆栈跟踪以及提供诸如调试器中的单步之类的功能非常有用。

关于java - javap命令的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20697091/

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