public String intern()Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true. All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification Returns: a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
如果你打印 new String("wo") == new String("wo") 和 new String("wo").intern() == new String("wo").intern() 你就会发现区别了。
public String intern()返回字符串对象的规范化表示形式。
一个初始时为空的字符串池,它由类 String 私有地维护。 当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用。 它遵循对于任何两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。 所有字面值字符串和字符串赋值常量表达式都是内部的。字符串字面值在《Java Language Specification》的 §3.10.5 中已定义。
返回:
一个字符串,内容与此字符串相同,但它保证来自字符串池中。
再补充介绍一点:存在于.class文件中的常量池,在运行期间被jvm装载,并且可以扩充。String的intern()方法就是扩充常量池的一个方法;当一个String实例str调用intern()方法时,java查找常量池中是否有相同unicode的字符串常量,如果有,则返回其引用,如果没有,则在常量池中增加一个unicode等于str的字符串并返回它的引用。
例3:
String s0=”kvill”;
String s1=new String(“kvill”);
String s2=new String(“kvill”);
System.out.println(s0==s1);
S1.intern();
S2=s2.intern();
System.out.println(s0==s1);
System.out.prntln(s0==s1.intern());
System.out.println(s0==s2);
结果为:
False
False //虽然执行了s1.intern(),但它的返回值没有赋给s1
True
True
最后再破除一个错误的理解:
有人说,“使用String.intern()方法可以将一个String类保存到一个全局的String表中,如果具有相同值的unicode字符串已经在这个表中,那么该方法返回表中已有字符串的地址,如果在表中没有相同值的字符串,则将自己的地址注册到表中”如果把这个全局的String表理解为常量吃的话,最后一句话“如果在表中没有相同值的字符串,则将自己的地址注册到表中”是错的。
例4:
String s1=new String(“kvill”);
String s2=s1.intern();
System.out.println(s1==s1.intern());
System.out.println(s1+” ”+s2);
System.out.println(s2==s1.intern());
结果是:
False
Kvill kvill
True
我们没有声明一个”kvill”常量,所以常量池中一开始没有”kvill”的,当我们调用s1.intern()后就在常量池中新添加了一个”kvill”常量,原来的不在常量池中的”kvill”仍然存在,也就不是“把自己的地址注册到常量池中”了。
例5:
String str1=”java”;
String str2=”blog”;
String s=str1+str2;
System.out.println(s==”javablog”);
结果是false。Jvm确实对型如String str1=”java”;的String对象放在常量池里,但是它是在编译时那么做的,而String s=str1+str2;是在运行时刻才能知道,也就是说str1+str2是在堆里创建的,所以结果为false了。
返回字符串对象的规范化表示形式。
一个初始时为空的字符串池,它由类 String 私有地维护。
当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用。
它遵循对于任何两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。
public String intern()返回字符串对象的规范化表示形式。
一个初始时为空的字符串池,它由类 String 私有地维护。 当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用。
它遵循对于任何两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。
所有字面值字符串和字符串赋值常量表达式都是内部的。字符串字面值在《Java Language Specification》的 §3.10.5 中已定义。 返回:
一个字符串,内容与此字符串相同,但它保证来自字符串池中。
A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true. All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification
Returns:
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
new String("wo").intern() == new String("wo").intern() 你就会发现区别了。
第一个是返回错误。
因为虽然他们内容相同, 但是他们是不同的Object, 你可以想象String类是 "x" 的wrapper class。第二个是返回正确。 因为如果你只在一个虚拟机上运行, 所有内容相同的String都只可能是一个String。然后用==比较速度快。 因为当你用intern()时, 你是去虚拟机的库藏里看是不是已经有了这样东西。如果有, 它就给你返回它仓库里已经有的。
返回字符串对象的规范化表示形式。
一个初始时为空的字符串池,它由类 String 私有地维护。
当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用。
它遵循对于任何两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。
public String intern()返回字符串对象的规范化表示形式。
一个初始时为空的字符串池,它由类 String 私有地维护。 当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用。 它遵循对于任何两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。 所有字面值字符串和字符串赋值常量表达式都是内部的。字符串字面值在《Java Language Specification》的 §3.10.5 中已定义。
返回:
一个字符串,内容与此字符串相同,但它保证来自字符串池中。
String str = "123";
调用str.intern()的时候,就是检查常量池中是否有"123".
如果有就返回"123"的引用.
没有的话就在常量池中增加"123"并且返回"123"的引用.