是否有任何方法或技术如何知道给定的 String s
已经在 String 池中? Java String
池如何以及何时创建?它包含哪些初始成员?
最佳答案
我的回答是:没有通用的解决方案。你可以做的是:
boolean wasAlreadyInterned = str.intern() == str;
但这有副作用,现在它肯定会被拘留。
JavaDoc String#intern
说,类 String 私下维护一个字符串池,该池最初是空的。
如果你看一下 String
类的实现,你所看到的就是
public native String intern();
Java Language Specification, Chapter 3.10.5, String literals说:
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
.
关于java - 如何找出给定的字符串已经在 Java 字符串池中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306709/