When and Where is the String initialised/stored in Java source code?

后端 未结 3 1690
梦毁少年i
梦毁少年i 2021-01-17 16:35

This is the source code I have:

public class Koray {
    public static void main(String [] args) {
            System.out.println(\"This is a sample program.         


        
3条回答
  •  青春惊慌失措
    2021-01-17 16:39

    ldc #3 loads the string constant stored in the constant pool, which is a separate table that stores constants such as String literals and class names.

    See http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4 from the JVM specification:

    The constant_pool is a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first "tag" byte.

    You can view the string table using javap -verbose ClassName.

    A sample output would be something like:

    Compiled from "Test.java"
    public class tests.Test extends java.lang.Object
      SourceFile: "Test.java"
      minor version: 0
      major version: 50
      Constant pool:
    const #1 = class        #2;     //  tests/Test
    const #2 = Asciz        tests/Test;
    const #3 = String        #4;     //  This is a sample program.
    ...
    

提交回复
热议问题