Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        6年前关闭。
                    
                
        

我使用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");
}

}


我不理解上述类文件中的以下内容:


堆栈= 4,当地人= 1,args_size = 1
LineNumberTable:部分

最佳答案

堆栈= 4,当地人= 1,args_size = 1


字节码中的值存储在局部变量插槽和操作数堆栈中。每个插槽最多可以具有2 ^ 16-1个插槽,但是出于效率方面的考虑,您需要在堆栈和本地变量上指定一个限制,这样即使您不实际使用它,也不会浪费所有空间。

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


  LineNumberTable:部分


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

07-24 09:49