Fundamentals 9 min read

Understanding Java Constant Pool: Theory and Practice

This article explains the Java constant pool, distinguishing static and runtime pools, demonstrates string interning behavior with code examples, and shows how to inspect class files and memory settings to observe constant pool effects in the JVM.

Java Captain
Java Captain
Java Captain
Understanding Java Constant Pool: Theory and Practice

Theory

The JVM memory layout includes the program counter, native method stack, virtual machine stack, method area, and heap. The method area holds the static constant pool (from .class files) and the runtime constant pool (loaded into the method area after class loading).

Java constant pools exist in two forms: the static constant pool stored in .class files, containing literals, class and method references, and the runtime constant pool created in the method area when classes are loaded.

String literals are stored in the runtime constant pool, and the intern() method can add strings to this pool.

1 String s1 = "Hello";
2 String s2 = "Hello";
3 String s3 = "Hel" + "lo";
4 String s4 = "Hel" + new String("lo");
5 String s5 = new String("Hello");
6 String s6 = s5.intern();
7 String s7 = "H";
8 String s8 = "ello";
9 String s9 = s7 + s8;

11 System.out.println(s1 == s2); // true
12 System.out.println(s1 == s3); // true
13 System.out.println(s1 == s4); // false
14 System.out.println(s1 == s9); // false
15 System.out.println(s4 == s5); // false
16 System.out.println(s1 == s6); // true

Using == compares object references, not content; String.equals() should be used for content comparison.

Key observations:

Compile‑time behavior (constant folding) determines whether literals share the same pool entry.

Runtime constant pool entries originate from class file constant pools unless manually added via intern() .

The JVM does not automatically add new constants to the pool during execution.

Other constant pools exist for primitive types (e.g., integer pool for values –128 to 127).

Practice

To inspect the static constant pool, compile a simple class like String s = "hi"; and view the resulting .class file with a hex editor. The file starts with the magic number CA FE BA BE , followed by the version, then the constant pool count and entries.

String s = "hi";

Each string constant entry begins with 01 , followed by a two‑byte length and the UTF‑8 bytes of the string (e.g., 01 00 02 68 69 for "hi").

Runtime constant pool resides in the method area. By setting JVM options such as -XX:PermSize=2M -XX:MaxPermSize=2M (pre‑JDK8) or -XX:MaxMetaspaceSize=2M (JDK8+), you can limit its size. A loop that continuously interns new strings quickly exhausts this space, resulting in java.lang.OutOfMemoryError: PermGen space or Metaspace errors, demonstrating that the runtime constant pool occupies the method area.

All code was tested on JDK7 and JDK8; behavior may vary with other versions.

Reference: "Deep Understanding of Java Virtual Machine – JVM Advanced Features and Best Practices".

JavaJVMMemory ManagementConstant PoolString Interning
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.