何时何地在Java源代码中初始化/存储String?

Kor*_*gay 10 java string jvm bytecode

这是我的源代码:

public class Koray {
    public static void main(String [] args) {
            System.out.println("This is a sample program.");
    } 
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,我得到字节码.当我用十六进制查看器查看字节码时,我看到部分:

19 54 68 69 73 20 69 73 20 61 20 73 61 6D 70 6C 65 20 70 72 6F 67 72 61 6D 2E
Run Code Online (Sandbox Code Playgroud)

可以读作

This is a sample program. 
Run Code Online (Sandbox Code Playgroud)

如果字节被解释为字符.

而当我这样做

javap -c Koray.class
Run Code Online (Sandbox Code Playgroud)

我拆开这个课我看到:

Compiled from "Koray.java"
public class Koray {
  public Koray();
    Code:
       0: aload_0       
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return        

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String This is a sample program.
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: bipush        10
      10: istore_1      
      11: return        
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,在反汇编的文本中看到的字符串在哪里?我只在评论中看到它.

Jeo*_*tan 5

看到那个ldc 指令了吗?它从运行时常量池加载常量。这就是你的字符串存储的地方。要也打印常量池,请向 javap 调用添加-verbose 选项。