String的intern方法
之前遇到过一个问题很值得思考
String s1 = new String("hello");
String intern1 = s1.intern();
String s2 = "hello";
System.out.println(s1 == s2); // 返回的是false
String s3 = new String("hello") + new String("hello");
String intern = s3.intern();
String s4 = "hellohello";
System.out.println(s3 == s4); // 返回的是true
使用的是JDK 8
第一个验证
看了很多资料,感觉还是有点懵逼。然后自己做了个实验来验证一下结果:
System.out.println(s1 == intern1); // false
System.out.println(intern1 == s2); // true
从这两个打印语句可以说明一个问题 :
- s1和intern1不是同一个对象的引用,s1是堆中地址的引用,intern1则是字符串常量池中的引用,故false
- 说明intern1 和 s2都是同一个对象的引用,都是在常量池中的。
String s2 = "hello"; // 存储在常量池中
这里可以知道,通过""创建的对象都是存储在常量池中的。所以s1 != s2
这里还有一点就是可能要注意的是: new String(“hello”)的时候,实际上会创建一个堆引用和一个常量池引用,而常量池的引用通过intern方法创建和获取。
代码解读:
// 创建了一个堆对象以及常量池的,一个堆中引用,一个常量池中的引用
String s1 = new String("hello");
// 获取常量池中的引用,发现上面已经创建好了。这一行代码其实要不要都无所谓
String intern1 = s1.intern();
// 获取常量池的引用
String s2 = "hello";
// 堆地址和常量池地址不匹配
System.out.println(s1 == s2); // 返回的是false
然后开始第二个验证
System.out.println(new String("hello") == new String("hello")); // false
System.out.println(new String("hello").intern() == new String("hello").intern()); // true
System.out.println(s3 == intern); // true
System.out.println(s4 == intern); // true
这两个打印语句说明:
- 通过new String 创建的对象,在堆中的位置是不同的。
- 通过intern方法发现他们在常量池中的对象却是相等的。
- 说明堆中的引用和常量池中的引用是一致的。
- 说明常量池中的引用就是指向堆的。
然后回到代码解读:
// 这里实际产生了三个对象2个hello和1个hellohello。其中常量池会存在一个hello,但是hellohello在常量池中是没有的
String s3 = new String("hello") + new String("hello");
// 获取常量池中的对象时发现没有hellohello这个引用,所以需要引用堆中的s3的地址
String intern = s3.intern();
// 这里创建s4的时候会去常量池中查找hellohello的引用。发现上面已经创建好了
String s4 = "hellohello";
System.out.println(s3 == s4); // 返回的是true
如果还是有点懵逼,可以结合上面4个打印语句再看看。
唬不唬得住你,我不知道,反正我是这么理解的。
我不要你觉得,我要我觉得。要么睡服我~~~ 哈哈哈
如果你理解的更透彻,也请指导指导,欢迎探讨!
参考文章 : https://blog.csdn.net/tyyking/article/details/82496901