What is the purpose of the Java Constant Pool?

Bober02 picture Bober02 · Apr 18, 2012 · Viewed 34.8k times · Source

I am currently trying to dig deeper into the specification of the Java Virtual Machine. I have been reading Inside the JVM book online and there is one confusing abstraction I can't seem to grasp: Constant Pool. here is the excerpt from the book:

For each type it loads, a Java virtual machine must store a constant pool. A constant pool is an ordered set of constants used by the type, including literals (string, integer, and floating point constants) and symbolic references to types, fields, and methods. Entries in the constant pool are referenced by index, much like the elements of an array. Because it holds symbolic references to all types, fields, and methods used by a type, the constant pool plays a central role in the dynamic linking of Java programs

I have several questions about the above and CP in general:

  1. Is CP located in .class file for each type?
  2. What does the author mean by "symbolic reference"?
  3. What is the Constant Pool's purpose, in simple English?

Answer

axtavt picture axtavt · Apr 18, 2012

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.