它遵循以下规则:对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。
String str1 = new String("123"); String str2 = "123"; String str3 = str1.intern(); System.out.println((str1 == str2) +","+ (str3 == str2)); String str4 = new String("234"); String str5 = new String("234"); String str6 = str4.intern(); String str7 = str5.intern(); System.out.println((str4 == str5) +","+ (str6 == str7));
运行结果:
false,true
false,true