当前位置:网站首页>The difference between string constants and string objects when allocating memory
The difference between string constants and string objects when allocating memory
2022-07-07 06:24:00 【Pei Nanwei_】
Catalog
Understand memory distribution
elicit questions
Before understanding the difference between the two , Let's take a look at a piece of code
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2);
String s3 = s2.intern();
System.out.println(s2 == s3);
}
If you were to 2 If you have doubts about the output , After reading this article , I believe you will get something .
Understand memory distribution
Let's first understand the creation process of string constants, string variables and their objects , I believe it will be convenient for you to understand the above comparison .
String constant : It is placed in the string constant pool , In the code, that is "abc"
String object : It's an object , It's in the pile , In the code, that is "new String("abc")"
Next is their distribution in memory
Explain and analyze :
Memory distribution analysis
String s1 = new String("abc")
First, open an address in the stack s1 In the pile new String("abc") The address of , then new This String The object of . stay new When this object , It is necessary to judge whether there is... In the string constant pool first "abc" This constant . If there is , Store the address of this constant directly in the object . without , You need to create this constant in the constant pool first , Then store the memory of this constant in the object .
String s2 = "abc"
First, open an address in the stack s2, The address of the constant to be stored . It is still to go to the constant pool to determine whether this constant exists , The difference is , This time, it is directly stored in the stack s1 quote .
String s3 = s2.intern()
Reference creation is as above ,String Object's intern Method , If the pool already contains one equal to this String Object's string ( The object is created by equals(Object) Method determination ), Then return the string in the pool . otherwise , Put this String Object added to pool , And return this String References to objects .
Result analysis
Compare s1 and s2 when , because s1 The object of is new Coming out , So it exists in the heap . and s2 The stored address is in the method area in the character constant pool , So it's more s1 and s2 Address comparison of , It must be false;
Compare s2 and s3 when , because s3 By s2.intern() Come to ,intern We already know that it is a reference to a constant returned directly . therefore s2 and s3 The storage address of is the same , Of course return true
Okay , That's all for this article , Favorite students can like the collection , Have a problem , Can comment , Or leave a message , I will give you feedback at the first time , Thank you for watching. !!
notes : This article is for me to share my learning experience , There are mistakes or areas that need to be corrected , Please correct me. , I will accept with an open mind
边栏推荐
- Jstack of JVM command: print thread snapshots in JVM
- 字符串常量与字符串对象分配内存时的区别
- Redisl garbled code and expiration time configuration
- 高并发大流量秒杀方案思路
- c语言面试写一个函数在字符串N中查找第一次出现子串M的位置。
- 对称的二叉树【树的遍历】
- LM小型可编程控制器软件(基于CoDeSys)笔记二十三:伺服电机运行(步进电机)相对坐标转换为绝对坐标
- Chain storage of stack
- 缓存在高并发场景下的常见问题
- Jstat of JVM command: View JVM statistics
猜你喜欢
A freshman's summary of an ordinary student [I don't know whether we are stupid or crazy, but I know to run forward all the way]
Haqi projection Black Horse posture, avec seulement six mois de forte pénétration du marché des projecteurs de 1000 yuans!
【OpenCV】形态学滤波(2):开运算、形态学梯度、顶帽、黑帽
Common problems of caching in high concurrency scenarios
Career experience feedback to novice programmers
如何在Touch Designer 2022版中设置解决Leap Motion不识别的问题?
rt-thread 中对 hardfault 的处理
Chain storage of stack
Vscode for code completion
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!
随机推荐
Crudini 配置文件编辑工具
UIC(组态UI工程)公版文件库新增7款行业素材
蚂蚁庄园安全头盔 7.8蚂蚁庄园答案
Symmetric binary tree [tree traversal]
Rk3399 platform development series explanation (WiFi) 5.53, hostapd (WiFi AP mode) configuration file description
Convert numbers to string strings (to_string()) convert strings to int sharp tools stoi();
JVM命令之 jstat:查看JVM統計信息
当我们谈论不可变基础设施时,我们在谈论什么
A freshman's summary of an ordinary student [I don't know whether we are stupid or crazy, but I know to run forward all the way]
jvm命令之 jcmd:多功能命令行
高并发大流量秒杀方案思路
win系统下安装redis以及windows扩展方法
A very good JVM interview question article (74 questions and answers)
JVM command - jmap: export memory image file & memory usage
Career experience feedback to novice programmers
matlab / ENVI 主成分分析实现及结果分析
如何在Touch Designer 2022版中设置解决Leap Motion不识别的问题?
字符串常量与字符串对象分配内存时的区别
Jstat of JVM command: View JVM statistics
ceres-solver和g2o性能比较