我在使用字符串创建和检查其哈希码进行测试时发现了一个有趣的案例.
在第一种情况下,我使用复制构造函数创建了
public class Test { /** * @param args */ public static void main(String[] args) { String s1 = new String("myTestString"); String s3 = s1.intern(); System.out.println("S1: " + System.identityHashCode(s1) + " S3:" + System.identityHashCode(s3)); } }
S1: 816115710 S3:478684581
这是预期的输出,因为实习字符串从字符串池中选择引用,而s1选择新对象的引用.所以他们的身份哈希码是不同的.
现在,如果我使用char数组创建String,那么我会看到一些奇怪的行为:
public class Test { /** * @param args */ public static void main(String[] args) { char[] c1 = { 'm','y','T','e','s','t','S','r','i','n','g' }; String s5 = new String(c1); String s6 = s5.intern(); System.out.println("S5: " + System.identityHashCode(s5) + " S6:" + System.identityHashCode(s6)); } }
S5: 816115710 S6:816115710
这是一个意想不到的输出. interned String和new String对象如何具有相同的identityhashcode?
有任何想法吗?
解决方法
在第一种情况下,myTestString文字在您调用实习生之前在池上,而在第二种情况下,它不是这样您的String s5直接放在池中.
如果我们一步一步地查看您的示例,则会发生以下情况:
> String s1 = new String(“myTestString”); =>使用String文字在池中创建一个String myTestString(让我们称之为s0),并且还创建一个新的String s1,它不在池中.
> String s3 = s1.intern(); =>检查池中是否有等效的String并找到s0.现在s3和s0指的是同一个实例(即s3 == s0为真,但s1!= s0).
在你的第二个例子中:
> String s5 = new String(c1);创建一个不在池中的新String
> String s6 = s5.intern();检查myTestString是否在池中但找不到,因此对实习生的调用会在池中创建一个新的String引用,该引用引用与s5相同的String.所以s6 == s5是真的.
最后你可以运行这两个程序来确认我的解释(第二个打印真实三次):
public static void main(String[] args) { String s1 = new String("myTestString"); String s3 = s1.intern(); System.out.println("myTestString" == s1); System.out.println(s3 == s1); System.out.println("myTestString" == s3); } public static void main(String[] args) { String s1 = new String(new char[] {'m','g'}); String s3 = s1.intern(); System.out.println("myTestString" == s3); System.out.println("myTestString" == s1); System.out.println(s3 == s1); }