Knowledge Transfer

Ethickfox kb page with all notes


Project maintained by ethickfox Hosted on GitHub Pages — Theme by mattgraham

When we compile a .java file, we get a separate class file with a .class extension. The .class file consists of several sections and a constant pool is one of them. constant pool contains the constants that are needed to run the code of a specific class. Basically, it's a runtime data structure similar to the symbol table. It is a per-class or per-interface runtime representation in a Java class file. The content of the constant pool consists of symbolic references generated by the compiler. These references are names of variables, methods, interfaces, and classes referenced from the code. The JVM uses them to link the code with other classes it depends on.

public class ConstantPool {
    
    public void sayHello() {
        System.out.println("Hello World");
    }
}
\#1 = Methodref          \#6.\#14         // java/lang/Object."<init>":()V
\#2 = Fieldref           \#15.\#16        // java/lang/System.out:Ljava/io/PrintStream;
\#3 = String             \#17            // Hello World
\#4 = Methodref          \#18.\#19        // java/io/PrintStream.println:(Ljava/lang/String;)V
\#5 = Class              \#20            // com/baeldung/jvm/ConstantPool
\#6 = Class              \#21            // java/lang/Object
\#7 = Utf8               <init>
\#8 = Utf8               ()V
\#9 = Utf8               Code
\#10 = Utf8               LineNumberTable
\#11 = Utf8               sayHello
\#12 = Utf8               SourceFile
\#13 = Utf8               ConstantPool.java
\#14 = NameAndType        \#7:\#8          // "<init>":()V
\#15 = Class              \#22            // java/lang/System
\#16 = NameAndType        \#23:\#24        // out:Ljava/io/PrintStream;
\#17 = Utf8               Hello World
\#18 = Class              \#25            // java/io/PrintStream
\#19 = NameAndType        \#26:\#27        // println:(Ljava/lang/String;)V
\#20 = Utf8               com/baeldung/jvm/ConstantPool
\#21 = Utf8               java/lang/Object
\#22 = Utf8               java/lang/System
\#23 = Utf8               out
\#24 = Utf8               Ljava/io/PrintStream;
\#25 = Utf8               java/io/PrintStream
\#26 = Utf8               println
\#27 = Utf8               (Ljava/lang/String;)V

#n indicates the references to the constant pool. #17 is a symbolic reference to the “Hello World” String, #18 is System.out, and #19 is a println. Similarly_,_ #8 highlights that the return type of method is void and #20 is a fully qualified class name.

The constant pool supports several types:

String instance in Java is an object with two fields: a char[] value field and an int hash field. The value field is an array of chars representing the string itself, and the hash field contains the hashCode of a string which is initialized with zero, calculated during the first hashCode() call and cached ever since. Important thing is that a String instance is immutable: you can’t get or modify the underlying char[] array. Another feature of strings is that the static constant strings are loaded and cached in a string pool. If you have multiple identical String objects in your source code, they are all represented by a single instance at runtime.