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

JVM如何在字符串常量池查找字符串?字面量未存在时的查找机制疑问

How Does JVM Look Up a String Literal That Doesn’t Exist in the String Constant Pool Yet?

Great question—this is one of those under-the-hood JVM details that feels counterintuitive at first glance, but it all clicks once you break down the process. Let’s walk through it step by step:

  • The key misunderstanding: You’re not looking for the object—you’re looking for its content
    The String Constant Pool (backed by ConcurrentHashMap) doesn’t store actual string objects as keys. Instead, it uses the character sequence (or a hashed representation of it) as the key, mapped to the reference of the String object. When the JVM processes your literal String literal = "Hello Java!", it doesn’t need the object to exist first—it just needs the content of the literal to perform the lookup.

  • The full flow from compile time to runtime
    Let’s break down what happens when your code is executed:

    1. Compile time: The literal "Hello Java!" is added to your .class file’s Constant Pool as a CONSTANT_String_info entry. This entry doesn’t hold the String object—it stores raw character data (or a reference to it) plus metadata like its precomputed hash value.
    2. Class loading: When your class is loaded into the JVM, the CONSTANT_String_info entries are moved to the Runtime Constant Pool. At this point, the String object still doesn’t exist in memory.
    3. Literal resolution: When the JVM encounters the line String literal = "Hello Java!", it triggers the intern() logic for the literal:
      • First, it uses the precomputed hash of the literal’s character sequence to check the ConcurrentHashMap (the String Constant Pool) for an existing entry.
      • If no entry exists: The JVM creates a new String object, then adds an entry to the map where the key is the literal’s content/hash, and the value is the reference to this new object.
      • Finally, it returns the reference (either the existing one or the new one) to assign to literal.
  • A real-world analogy to make it stick
    Think of the String Constant Pool like a library’s catalog:

    • When you ask for "Hello Java!", you’re giving the librarian the title (the character sequence), not the book itself.
    • The librarian checks the catalog (the ConcurrentHashMap) for that title.
    • If the book exists in the catalog, they point you to its location (the object reference).
    • If not, they order a new copy (create the String object), add it to the shelves, update the catalog with the title and location, then point you to it.
  • Bonus: Why ConcurrentHashMap is the right choice
    The switch from a fixed-size array (in early JVM versions) to ConcurrentHashMap was all about efficiency. The map allows for O(1) average lookup time using the content’s hash, and it handles concurrent access safely—critical since multiple threads might be resolving string literals at the same time.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:22:49