当前位置:网站首页>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
边栏推荐
- VIM mapping large K
- Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
- On the discrimination of "fake death" state of STC single chip microcomputer
- C面试24. (指针)定义一个含有20个元素的double型数组a
- laravel 使用腾讯云 COS5全教程
- Jstat of JVM command: View JVM statistics
- C note 13
- Crudini 配置文件编辑工具
- Common problems of caching in high concurrency scenarios
- Calculation model FPS
猜你喜欢
生活中的开销,怎么记账合适
window下面如何安装swoole
Laravel uses Tencent cloud cos5 full tutorial
"Parse" focalloss to solve the problem of data imbalance
SubGHz, LoRaWAN, NB-IoT, 物联网
软件测试知识储备:关于「登录安全」的基础知识,你了解多少?
博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生
JVM命令之- jmap:导出内存映像文件&内存使用情况
3428. 放苹果
PostgreSQL database timescaledb function time_ bucket_ Gapfill() error resolution and license replacement
随机推荐
Calculation model FPS
博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生
When we talk about immutable infrastructure, what are we talking about
Deep clustering: joint optimization of depth representation learning and clustering
微信小程序隐藏video标签的进度条组件
win系统下安装redis以及windows扩展方法
Qtthread, one of many methods of QT multithreading
VIM mapping large K
Apple CMS V10 template /mxone Pro adaptive film and television website template
Ant manor safety helmet 7.8 ant manor answer
Implementation of VGA protocol based on FPGA
Check point: the core element for enterprises to deploy zero trust network (ztna)
计算模型 FPS
外设驱动库开发笔记43:GPIO模拟SPI驱动
uniapp开发小程序如何使用微信云托管或云函数进行云开发
Shared memory for interprocess communication
Jinfo of JVM command: view and modify JVM configuration parameters in real time
window下面如何安装swoole
Sequential storage of stacks
Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning