天天看點

String的intern方法

Class 常量池 Constant Pool

存在于編譯生成的 Class 檔案中。用于存在各種字面量、符号引用。

方法區

方法區在 JVM 啟動時建立,裡面包含運作時常量池、靜态變量、方法的代碼等。

方法區是堆的邏輯組成部分,但其實作要看具體 JVM 是存放到哪的,而且也沒有規定是否需要對方法區進行垃圾回收。

運作時常量池 Runtime Constant Pool

JVM 規範中的定義:

A run-time constant pool is a per-class or per-interface run-time representation of the

constant_pool

table in a

class

file. It contains several kinds of constants, ranging from numeric literals known at compile-time to method and field references that must be resolved at run-time.

即每個類都有它的運作時常量池,運作時常量池是 Class 檔案中常量池的運作時表現形式。

類加載時會建立它的運作時常量池,并存放 Class 常量池中符号引用。并且在解析時的直接引用也會放進運作時常量池。

注:在 Hotspot,Java 7 開始把這些符号引用從 PermGen 改為存放到 native memory。

全局字元串常量池 String Pool

JVM 在運作時使用的全局的 StringTable(就是一個哈希表)。Hotspot 從 Java 7 開始,存放于堆裡。

String的intern方法