java中的Intern()是什么?

java中的Intern()是什么?,java,string-interning,Java,String Interning,intern()的用途是什么?只要阅读Java文档,它就会告诉您: 返回字符串对象的规范表示形式 最初为空的字符串池由类字符串私下维护 调用intern方法时,如果池中已经包含一个字符串,该字符串等于equals(object)方法确定的该字符串对象,则返回池中的字符串。否则,此字符串对象将添加到池中,并返回对此字符串对象的引用 因此,对于任意两个字符串s和t,s.intern()==t.intern()为真当且仅当s.equals(t)为真时 所有文字字符串和字符串值常量表达式都被插入。Jav

intern()的用途是什么?

只要阅读Java文档,它就会告诉您:

返回字符串对象的规范表示形式

最初为空的字符串池由类字符串私下维护

调用intern方法时,如果池中已经包含一个字符串,该字符串等于equals(object)方法确定的该字符串对象,则返回池中的字符串。否则,此字符串对象将添加到池中,并返回对此字符串对象的引用

因此,对于任意两个字符串s和t,s.intern()==t.intern()为真当且仅当s.equals(t)为真时

所有文字字符串和字符串值常量表达式都被插入。Java语言规范§3.10.5中定义了字符串文字


谷歌会给你一个很好的结果,如果你要搜索…什么你会发现很难在谷歌找到,你应该问这里。
public static void main(String[] args)
{
    String literalstr = "ABC";
    String literalstr2 = "ABC";
    String str = new String("ABC");
    String str2 = new String("ABC"); 

if (literalstr == literalstr2)
{
     System.out.println("Literal String... I use String Pooling");
}
if (str != str2)
{
      System.out.println("Object String... I dont use String Pooling");
}
if (str.intern() == str2.intern())
{
     System.out.println("Interning ... I use String Pooling");
}
    // System.out.println(ric2);
}