如何检查字符串池内容?

编程入门 行业动态 更新时间:2024-10-11 11:20:04
本文介绍了如何检查字符串池内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

如何检查字符串池内容?

本文发布于:2023-11-12 13:32:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581641.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!