如何检查字符串池内容? [英] How to check String Pool Contents?

查看:129
本文介绍了如何检查字符串池内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查,目前字符串池中有哪些字符串。

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 是一种原生方法,正如我们在< a href =http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java#l3152>其在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.

这篇关于如何检查字符串池内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆