有没有办法检查,目前字符串池中有哪些字符串。
Is there any way to check, currently which Strings are there in the String pool.
我能以编程方式列出池中存在的所有字符串吗?
Can I programmatically list all Strings exist in pool?
或
任何IDE都有这种插件吗?
Any IDE already have this kind of plugins ?
推荐答案您无法从Java代码访问字符串池,至少不能在Java VM的HotSpot实现中访问。
You are not able to access the string pool from Java code, at least not in the HotSpot implementation of Java VM.
Java中的字符串池是使用字符串实习实现的。根据JLS§3.10.5:
String pool in Java is implemented using string interning. According to JLS §3.10.5:
字符串文字总是指同一个类 String 的实例。这是因为字符串文字 - 或者更常见的是作为常量表达式(第15.28节)的值的字符串 - 被实现以便使用方法 String.intern 。
a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.
根据JLS§15.28:
编译时类型 String 的常量表达式总是实例化,以便使用 String.intern 方法共享唯一实例。
Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.
String.intern 是一种原生方法,正如我们在其在OpenJDK中的声明:
String.intern is a native method, as we can see in its declaration in OpenJDK:
public native String intern();此方法的本机代码调用 JVM_InternString 功能。
The native code for this method calls JVM_InternString function.
JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str)) JVMWrapper("JVM_InternString"); JvmtiVMObjectAllocEventCollector oam; if (str == NULL) return NULL; oop string = JNIHandles::resolve_non_null(str); oop result = StringTable::intern(string, CHECK_NULL); return (jstring) JNIHandles::make_local(env, result); JVM_END也就是说,字符串实习是使用本机代码实现的,并且没有Java API直接访问字符串池。但是,您可以为此目的自行编写本机方法。
That is, string interning is implemented using native code, and there's no Java API to access the string pool directly. You may, however, be able to write a native method yourself for this purpose.
更多推荐
如何检查字符串池内容?
发布评论