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

后端 未结 3 1692
梦毁少年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:42

    ldc #3; //String This is a sample program.
    

    This line uses the opcode ldc, which loads a constant onto the operand stack. At this point, we're going to load whatever constant is in index #3 of our constant pool table.

    The constant pool table is where most of the literal constant values are stored. (ref) The command javap -c -verbose provides the output for constant pool table.

    Example output:

    const #22 = String      #23;    // This is a sample program 
    const #23 = Asciz       This is a sample program;
    

    More info from VM Spec ( http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html ):

    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. The constant_pool table is indexed from 1 to constant_pool_count-1.

提交回复
热议问题