Fundamentals 3 min read

Unlocking Java’s Constant Pools: Class, Runtime, and String Explained

This article explains Java’s constant pools, detailing the class constant pool stored in .class files, its transformation into the runtime constant pool during JVM loading, and the specialized string constant pool that optimizes memory usage, including recent changes moving it to heap memory.

Lobster Programming
Lobster Programming
Lobster Programming
Unlocking Java’s Constant Pools: Class, Runtime, and String Explained

1. Class Constant Pool

The class constant pool is part of a Java .class file; the compiler generates it when compiling source code. It stores literals such as strings and numeric constants, as well as symbolic references like class, field, and method names and descriptors. The following image shows the class constant pool after decompiling with javap :

The class constant pool resides at the beginning of each class file and contains all information required for the class or interface to run on the Java platform. It is static and holds every dependency so that the class loader can resolve them at runtime.

2. Runtime Constant Pool

The runtime constant pool is the in‑memory representation of the class constant pool after the class is loaded into the JVM. During class loading, the information from the class constant pool is copied into the runtime constant pool.

Conceptually, the class constant pool and the runtime constant pool are the same; the former exists in the .class file, while the latter exists in the method area of the JVM. When a class is loaded, its literals are placed in the method area, consuming memory, which is referred to as the runtime constant pool.

3. String Constant Pool

The string constant pool is a memory region dedicated to storing string literals. When a new string literal is declared, the JVM checks the string constant pool; if the literal already exists, it returns the existing reference instead of creating a new object.

The string constant pool is a specialized part of the runtime constant pool for managing strings. Since Java 7, the string constant pool has been moved to the heap, reducing pressure on the method area and improving garbage collection.

JavaJVMMemory ManagementConstant PoolString Interning
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.