以前没有注意到这个方法,看effective Java时候发现的,记录下来。
当且仅当 s.equals(t)
为 true
时,s.intern() == t.intern()
才为 true
。
String (Java Platform SE 6)<!-- Generated by javadoc (build 1.6.0-beta2) on Mon Mar 19 18:23:04 CST 2007 --> <script type="text/javascript"> function windowTitle() { if (location.href.indexOf('is-external=true') == -1) { parent.document.title="String (Java Platform SE 6)"; } } </script>
<noscript></noscript>public String intern ()
-
返回字符串对象的规范化表示形式。
一个初始为空的字符串池,它由类
String
私有地维护。当调用 intern 方法时,如果池已经包含一个等于此
String
对象的字符串(用equals(Object)
方法确定),则返回池中的字符串。否则,将此String
对象添加到池中,并返回此String
对象的引用。它遵循以下规则:对于任意两个字符串
s
和t
,当且仅当s.equals(t)
为true
时,s.intern() == t.intern()
才为true
。所有字面值字符串和字符串赋值常量表达式都使用 intern 方法进行操作。字符串字面值在 Java Language Specification 的 §3.10.5 定义。
-
-
返回:
- 一个字符串,内容与此字符串相同,但一定取自具有唯一字符串的池