Java String.intern()方法学习,字符串池学习
字符串在开发中随处可见,但是String.intern()方法我却鲜有使用,这里进行一个查缺补漏。
作者学习基于openjdk1.8版本进行学习,而非jdk1.8(现在属于Oracle JDK),因此在部分源码会有所区别,如:openjdk的
sun.misc.Version#launcher_name
值为“openjdk”,oraclejdk则为“java”。
关于intern()方法
这里我是通过阅读源码及相关注释来学些的
通过IDE点击进入String.intern()
,我发现该方法被native
关键词修饰了,这使得该方法可以调用本地应用/或库(这里不展开说)。
/**
* Returns a canonical representation for the string object.
* <p>
* A pool of strings, initially empty, is maintained privately by the
* class {@code String}.
* <p>
* When the intern method is invoked, if the pool already contains a
* string equal to this {@code String} object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this {@code String} object is added to the
* pool and a reference to this {@code String} object is returned.
* <p>
* It follows that for any two strings {@code s} and {@code t},
* {@code s.intern() == t.intern()} is {@code true}
* if and only if {@code s.equals(t)} is {@code true}.
* <p>
* All literal strings and string-valued constant expressions are
* interned. String literals are defined in section 3.10.5 of the
* <cite>The Java™ Language Specification</cite>.
*
* @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/
public native String intern();
通过阅读方法上的注释,我了解到:
String
有一个“字符串池”,最初是空的,并由被String类独自维护。- 当调用
intern()
方法时,如果池中已经包含一个等于该String对象的字符串,该字符串由equals(Object)
方法判断,为返回值为true
时返回池中的字符串。否则,将此String对象添加到池中并返回对该String对象的引用。 - 因此,对于任何两个字符串“s”和“t” ,当且仅当
s.equals(t)
为true
时,s.intern() == t.intern()
才为true
。
实践
final String str1 = new StringBuilder("chan").append("jx").toString();
// 输出:chanjx
System.out.println(str1);
// 输出:chanjx
System.out.println(str1.intern());
// 输出:true
System.out.println(str1 == str1.intern());
final String str2 = new StringBuilder("open").append("jdk").toString();
// 输出:openjdk
System.out.println(str2);
// 输出:openjdk
System.out.println(str2.intern());
// 输出:false
System.out.println(str2 == str2.intern());
结论
在一个String
被创建时,若这个字符串不在“字符串池”时,它会被加入“字符串池”,若已存在则从“字符串池”内获取。