You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java 6 String内存模型:StringPool存储及字面量与对象差异问询

String常量池与字面量、String对象的区别解析

Great question—this is a common point of confusion for Java developers, so let’s unpack it clearly.

一、String常量池中实际存储的是什么?

When you write String text = "Hello World";, here’s what happens under the hood:

  • At class load time, the JVM parses the string literal "Hello World" and creates a String object with that value.
  • This String object (or a reference to it, in Java 7+) is stored in the string constant pool—a special memory area designed to optimize string usage by reusing instances.
  • When the line String text = "Hello World"; executes, the JVM first checks if the constant pool already contains a String object with the value "Hello World". If yes, it assigns that existing object’s reference to text. If not, it creates the object, adds it to the pool, then assigns the reference.

So to correct the common misconception: the constant pool doesn’t store "literals" as raw text—it stores String objects (or references to them) that correspond to your code’s string literals.

二、字符串字面量与String对象的核心区别

Let’s break down the key differences between these two concepts, even though both rely on a char[] (or byte[] in Java 9+) under the hood:

1. They exist at different stages

  • String literal: This is a source-code concept—it’s the sequence of characters you write between double quotes (e.g., "Hello World") in your Java code. It’s just syntax until the compiler and JVM process it.
  • String object: This is a runtime entity—an instance of the java.lang.String class that lives in memory (either the constant pool-associated heap area or the regular heap). It’s the actual data structure holding the character array and other metadata like hash code.

2. Storage & reuse behavior

  • Literal-derived String objects: Created by the JVM during class loading, these are automatically added to the constant pool and reused across your code. Multiple variables assigned the same literal will point to the exact same object:
    String a = "Hello";
    String b = "Hello";
    System.out.println(a == b); // Prints true—same object reference
    
  • Other String objects: Created via new String(...), StringBuilder.toString(), or other methods, these live in the regular heap and are not added to the constant pool by default. Each new String(...) call creates a new instance, even if the content matches an existing pool object:
    String c = new String("Hello");
    System.out.println(a == c); // Prints false—different object references
    System.out.println(a.equals(c)); // Prints true—same content
    

3. Underlying array sharing (Java 8 and earlier)

While both types of String objects use a char[] for storage, literal-derived objects may have their char arrays shared with other String instances. For example, when you create new String("Hello"), the new object’s char[] references the same array as the constant pool’s "Hello" object. This is an optimization the JVM uses to save memory, though it’s an implementation detail you rarely need to worry about. Java 9+ changed this to use byte arrays for better memory efficiency, but the core reuse logic remains.

总结

To wrap up:

  • The string constant pool stores reusable String objects (or references to them) that come from your code’s string literals.
  • String literals are just source-code syntax, while String objects are the runtime representations. The "difference" lies in how they’re created, stored, and reused by the JVM—even though they share the same underlying character storage mechanism.

内容的提问来源于stack exchange,提问作者gaurs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:46:41