当前位置:网站首页>关于字符串常量池,intern方法的理解
关于字符串常量池,intern方法的理解
2022-04-22 19:01:00 【李昕羽】
-
首先确定一点无论是jdk1.6还是1.7
String c = "ab";这种字面赋值都是直接在字符串常量池生成一个字符串对象的。
-
String a = new String("a"); String b = "a"; System.out.println(a == b);输出的是
false。
这说明在用new生成字符串对象的时候,只会在堆中生成字符串对象。而不会在字符串常量池生成对象,也没有引用。 -
明确:
1、在jdk1.6中,intern()方法是先查找字符串常量池是否含有当前字符串,如果没有,那么就在字符串常量池中创建 该字符串,并且返回该字符串在字符串常量池中的引用。
2、在jdk1.7中,intern()方法也是先查找字符串常量池中是否含有当前字符串,如果没有,将该字符串在堆的引用存在常量池中。并且返回该字符串在堆中的引用。
这是因为在jdk1.6中字符串常量池在永生代(方法区)中,而在jdk1.7字符串常量池转移到了堆中。
《深入理解Java虚拟机:JVM高级特性与最佳实践(第3版) (华章原创精品)》书中原话。

上述即可解释下面的例子:

需要注意的是,这里看作之前字符串常量池就有java这个字符串了。
-
看下面示例
String a = new String("ab"); String b = new String("ab"); String c = "ab"; String d = "a" + "b"; String e = "b"; String f = "a" + e; System.out.println(b.intern() == a); System.out.println(b.intern() == c); System.out.println(b.intern() == d); System.out.println(b.intern() == f); System.out.println(b.intern() == a.intern());输出结果为
false true true false true由运行结果可以看出来,b.intern() == a和b.intern() == c可知,采用new 创建的字符串对象不进入字符串池,并且通过b.intern() == d和b.intern() == f可知,字符串相加的时候,都是静态字符串的结果会添加到字符串池,如果其中含有变量(如f中的e)则不会进入字符串池中。但是字符串一旦进入字符串池中,就会先查找池中有无此对象。如果有此对象,则让对象引用指向此对象。如果无此对象,则先创建此对象,再让对象引用指向此对象。
5.我们看下面示例:这个类似于我们上面在书中看到的那个例子

输出是false,但是按照我们上述的推理来说他应该输出的true才对。
于是我查阅各种资料发现可能是因为字符串a在常量池中已经被创建过了。尝试下面写法:
String str1 = new String(new char[]{
'a'});
System.out.println(str1.intern() == str1);
输出的是True。
灰色的应该就是字符串常量池已经有这个字符串了
版权声明
本文为[李昕羽]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_54494937/article/details/122557794
边栏推荐
- 被删除的相片能恢复吗?3个技巧恢复被删除的相片
- nodejs如何预防xss攻击
- Take you to understand the principle of highly flexible spark architecture
- 2020-10-26 go语言grpc学习
- Data time-sharing market, data backup and individual stock rise and fall development of today's index items [11]
- [spark] (task6) spark RDD completes the statistical logic
- Introduction to high performance key value database redis
- Nodejs file operation
- SQL命令 DISTINCT
- 配置拦截器不拦截Swagger
猜你喜欢

B03 based on STM32 single chip microcomputer independent key control nixie tube stopwatch Proteus design, Keil program, C language, source code, standard library version

STC current interrupt list of all series

leetcode151. Reverse the words in the string (thought + explanation)

Pattern machine template computer CAD free pattern drawing and format conversion software ps300b tutorial: general CAD drawing and pattern drawing of Japanese brother pattern machine DXF file transfer

RHCE-ansible

Can deleted photos be restored? 3 tips to restore deleted photos

The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video

System Analyst - paper writing framework construction

Alibaba微服务组件Sentinel介绍

XML file input of Chapter 13 of kettle paoding jieniu
随机推荐
Nat. Commun. | Neural coding framework for learning generation model
高性能的key-value数据库Redis介绍
Better than SQL, another domestic database language was born
Take you to understand the principle of highly flexible spark architecture
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
CDATA drivers for JIRA complete features and attributes
配置拦截器不拦截Swagger
Network security -- the use of burp suite packet capture tool
[best practice] patrol item: turn on URL authentication in content distribution network (CDN)
Nodejs file operation
Cross chain asset interaction -- how to transfer KSM on Moonriver
SQL command distinct
漂亮舒服的KN95口罩,防护能力也很强
通关秘籍
Simple implementation of linear regression
单片机红外模块知识分享,理论是日后实战的基础
RHCE-ansible
Im instant messaging development how to design a database that can support millions of concurrent users
Bluetooth shield / cervical massage instrument / mini charger / probe thermometer, segment code LCD display driver ic-vk1088b qfn32 4 * 4 ultra small volume, 22seg * 4com, and can enter the power savi
Why can't async be used directly in useeffect