当前位置:网站首页>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
边栏推荐
- laravel 使用腾讯云 COS5全教程
- How to keep accounts of expenses in life
- FlexRay通信协议概述
- Peripheral driver library development notes 43: GPIO simulation SPI driver
- Jstack of JVM command: print thread snapshots in JVM
- VMware安装后打开就蓝屏
- How to set up in touch designer 2022 to solve the problem that leap motion is not recognized?
- 【GNN】图解GNN: A gentle introduction(含视频)
- Jcmd of JVM command: multifunctional command line
- 2022Android面试必备知识点,一文全面总结
猜你喜欢
CloudCompare-点对选取
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
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]
蚂蚁庄园安全头盔 7.8蚂蚁庄园答案
ETCD数据库源码分析——从raftNode的start函数说起
Jcmd of JVM command: multifunctional command line
基于FPGA的VGA协议实现
laravel 使用腾讯云 COS5全教程
3428. 放苹果
Chain storage of stack
随机推荐
Qt多线程的多种方法之一 QThread
安装VMmare时候提示hyper-v / device defender 侧通道安全性
win系统下安装redis以及windows扩展方法
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
Ant manor safety helmet 7.8 ant manor answer
Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
Three updates to build applications for different types of devices | 2022 i/o key review
ETCD数据库源码分析——从raftNode的start函数说起
How to keep accounts of expenses in life
【GNN】图解GNN: A gentle introduction(含视频)
K8s running Oracle
Database notes 04
Jstat pour la commande JVM: voir les statistiques JVM
Software testing knowledge reserve: how much do you know about the basic knowledge of "login security"?
基于FPGA的VGA协议实现
JVM监控及诊断工具-命令行篇
postgresql 数据库 timescaledb 函数time_bucket_gapfill()报错解决及更换 license
线性代数(一)
Rk3399 platform development series explanation (interruption) 13.10, workqueue work queue
Developers don't miss it! Oar hacker marathon phase III chain oar track registration opens