Sub topic 3
The Constant Pool and Symbolic References
This chapter assumes you can compile basic Java programs and use javac, java, and javap. You should also know what classes, methods, fields, and static members are.
The constant pool is a table inside every Java class file. It stores values and descriptions that the bytecode needs while the JVM loads and runs the class. The table avoids repeating the same text and lets the class file refer to classes, methods, and fields symbolically.
What the constant pool contains
A constant-pool entry has a tag that identifies its kind. An entry can contain a number, a string, or an index pointing to other entries.
| Entry kind | Purpose |
|---|---|
| `Utf8` | Stores text such as a class name, method name, or descriptor |
| `Class` | Refers to a class or interface name |
| `NameAndType` | Combines a member name with its type descriptor |
| `Methodref` | Refers to a class and an ordinary method |
| `Fieldref` | Refers to a class and a field |
| `String` | Refers to a string literal |
The constant pool is indexed. Index zero is reserved, so valid entries start at index 1. Some entries, such as Long and Double, use two consecutive index positions.
Consider this class:
public class MathOps {
public static int add(int a, int b) {
return a + b;
}
}Compile it and inspect it:
$ javac MathOps.java
$ javap -verbose MathOps
...
Constant pool:
#1 = Methodref #2.#3
#2 = Class #4
#3 = NameAndType #5:#6
#4 = Utf8 "java/lang/Object"
#5 = Utf8 "<init>"
#6 = Utf8 "()V"
#7 = Class #8
#8 = Utf8 "MathOps"
...The exact numbering can differ, but the relationships are important. Entry #1 describes a method reference. It points to class entry #2 and name-and-type entry #3. Entry #2 identifies java/lang/Object. Entry #3 identifies the constructor name <init> and descriptor ()V.
Symbolic references
A symbolic reference names something without storing its final memory address. For example, a method reference says, in effect, “the method named add with descriptor (II)I in class MathOps.” The JVM resolves that description to a concrete method during linking or execution.
A descriptor uses compact JVM type codes:
Imeansint.Vmeansvoid.()means no parameters.(II)Imeans twointparameters and anintresult.Ljava/lang/String;meansString.
This indirection allows compiled classes to refer to classes that were not assigned runtime addresses when compilation occurred. It also supports separate compilation and dynamic class loading.
Resolution and common mistakes
When the JVM resolves a symbolic reference, it checks whether the named class, method, or field exists and whether access rules allow its use. Resolution can fail if a dependency is missing, a method has the wrong descriptor, or a class was loaded by an incompatible class loader.
Do not confuse a string literal with a method reference. Both can appear in the constant pool, but a String entry refers to text, while a Methodref entry describes executable behavior.
Exercise
In the descriptor (Ljava/lang/String;I)V, what are the parameter types and return type?
Show answer
The method accepts a String and an int, and returns void.