What is the purpose of the Java Constant Pool?
Constant pool is a part of .class
file (and its in-memory representation) that contains constants needed to run the code of that class.
These constants include literals specified by the programmer and symbolic references generated by compiler. Symbolic references are basically names of classes, methods and fields referenced from the code. These references are used by the JVM to link your code to other classes it depends on.
For example, the following code
System.out.println("Hello, world!");
produces the following bytecode (javap
output)
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String Hello, world!
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
#n
here are references to the constant pool. #2
is a symbolic reference to System.out
field, #3
is a Hello, world!
string and #4
is a symbolic reference to PrintStream.println(String)
method.
As you can see, symbolic references are not just names - for example, symbolic reference to the method also contains information about its parameters (Ljava/lang/String;
) and return type (V
means void
).
You can inspect constant pool of a class by running javap -verbose
for that class.
I think understanding how the frame is constructed using a diagram would help.
The frame is where the operands (operation instructions) reside and that is where the dynamic linking occurs. It is a shorthand way, so to speak, using the constant pool to keep track of the class and it's members.
Each frame contains a reference to the runtime constant pool. The reference points to the constant pool for the class of the method being executed for that frame. This reference helps to support dynamic linking.
C/C++ code is typically compiled to an object file then multiple object files are linked together to product a usable artifact such as an executable or dll. During the linking phase symbolic references in each object file are replaced with an actual memory address relative to the final executable. In Java this linking phase is done dynamically at runtime.
When a Java file is compiled, all references to variables and methods are stored in the class's constant pool as a symbolic reference. A symbolic reference is a logical reference not a reference that actually points to a physical memory location.
Here is a link to James Blooms JVM Internals for more details.