Java string constant pool

Keywords: Java Attribute JDK jvm

Character string

  • Literal amount of string: refers to the string itself, such as "Java", "Hello".
  • String object: for example, new String("abc") or direct String s="str". The following "str" is also a string object.
  • String reference: a reference is a variable that points to the corresponding string object.

Constant pool

class constant pool

The class file generated after Java source file compilation includes the constant pool, which holds literal and symbol references, such as

public class JvmClass1 {
    final int b=666;
    public static void main(String[] args) {
        String c="java";
        String d="abcd";

In the corresponding class file

This term is 666.

These two items are the literal quantities of java and abcd strings.

Symbol references are also constants, such as fully qualified class names, field names and descriptors, method names and descriptors.

This is the class name.

This is the variable name.

There are some constant types in the constant pool that have an index attribute, pointing to another literal quantity, such as constant [class] info, constant [string] info, etc.
Only one copy of the same string literal will be saved in the constant pool, for example

public class JvmClass1 {
    public static void main(String[] args) {
        String c="java";
        String d="abcd";
        String e="java";
        String f=new String("java");

Runtime constant pool & & string constant pool

After the class constant pool is loaded into memory, it forms a runtime constant pool. Before Jdk1.7, it was in the method area, after Jdk1.8, it was in the meta space, or it was regarded as a new method area.
The runtime constant pool is dynamic compared with the class constant pool. Java does not require all constants to be generated in the compiler. It can generate constants at runtime and add them to the constant pool, such as the String class's inter().


The annotation of the intern source code is Returns a canonical representation for the string object.
The second section is a pool of strings, initially empty, is maintained private by the class, which means string constant pool. JDK 1.6 and earlier were placed in the method area and later in the heap, where the reference of string object is saved, while the real string object instance is created in the heap.
The third paragraph is

When the intern method is invoked, if the pool already contains a string equal to this {@code String} object 
as determined by the {@link #equals(Object)} method, then the string from the pool is returned. Otherwise, 
this {@code String} object is added to the pool and a reference to this {@code String} object is returned.

When a String object calls the inter method, if there is already a String constant with equal value in the pool (compared by the equal function of String), it returns the constant in the constant pool, that is, the reference of the corresponding instance in the heap. Otherwise, add the String to the constant pool.
for example

public class JvmClass1 {
    public static void main(String[] args) {
        String a = "hello";
        String b = new String("hello");
        System.out.println(a == b);//false a and b are different objects
        String c = "world";
        System.out.println(c.intern() == c);//true c.intern() returns the reference of "world" in the constant pool, which is the same object as C
        String d = new String("mike");
        System.out.println(d.intern() == d);//false d.intern() returns similar a, while D is similar to b, different objects
        String e = new String("jo") + new String("hn");
        System.out.println(e.intern() == e);//true is obtained by splicing. There is no literal amount of "john". Therefore, only when e.intern() is added to the pool, it is the same object
        String f = new String("ja") + new String("va");
        System.out.println(f.intern() == f);//false there is a blog that says "java" is automatically added to the string constant pool when the jvm starts, but no other evidence has been found.

Posted by raw100 on Wed, 29 Jan 2020 04:59:03 -0800