当前位置:网站首页>In depth analysis of the source code, why is the string class immutable? (hit me before you understand)
In depth analysis of the source code, why is the string class immutable? (hit me before you understand)
2022-07-26 21:48:00 【Work hard, work hard, gzc】
One 、 What is immutable ?
String Immutability is actually very simple , As shown in the figure below , For an existing string s=“abcd”, Assign it a second time to "abcdel", This operation will not be in the original memory address ( namely “abcd” Memory address of ) Modify the data on , Instead, it points to a new memory address ( namely “abcdel”). Therefore, it is not what we generally think that the dove occupies the magpie's nest ——“abcdel” To replace the “abcd”, Occupy “abcd” Memory location for , Lead to “abcd” homeless , Disappear from this world .
Two 、String The immutable nature
Check out jdk in String Class source code :
( Only the first three lines are listed here , If you are interested, you can check the complete source code by yourself )
We found that
( One )String Class is a final class .
String Class by final modification , We all know when final When modifying a class , This class cannot be inherited by other classes , natural String Class has no subclasses , There is also no saying that methods are overridden by subclasses , So this guarantees The outside world cannot inherit String class , To achieve the right String The destruction of immutability .
( Two )String The bottom is through a char[] To store data , And the char[] from private final modification .
The value The array is final modification , We know By final Variables of modified reference types can no longer point to other objects , in other words value An array can only point to its own array in the heap , You can no longer point to other arrays . But this does not prevent us from changing the contents of the array it points to , For example, let's give a random example :

Then the external saboteurs may think , Since you won't let me change value The point of the array , Then I can modify the contents of it , Hey
Then he thought about visiting this value Array to modify the values inside , But when operating, he found , He . Don't come out of this value Array , He wondered , How can you not visit ?
In fact, this time is private work , With private Naturally, the outside world can't visit me value Array , Naturally, I can't modify the data inside .
Okay , Now the outside wants to destroy us String The road of non variability is blocked , It also has to ensure that there is no internal “ spy ” ah , Don't make jokes, it's not !
( 3、 ... and )String Class does not provide or expose anything that can be changed value Array method .
Looking at the source code , We found that String The interior of the class does not provide or expose anything that can be changed value Method of array contents , Therefore, the internal safety is guaranteed , In this way, the exterior and interior are impeccable . This has reached us String The ultimate goal of class immutability !
3、 ... and 、 summary
Guarantee String Class immutability depends on the following three points :
( One )String Class quilt final Modification causes it not to be inherited , Thus avoiding subclass destruction String Immutability .
( Two ) Save string value The array is final Decorated and private .
( 3、 ... and )String Class does not provide or expose modification of this value Array method .
边栏推荐
- 按关键字搜索易贝商品 API
- 攻防世界----ics-07
- 织梦文档关键词维护不管用
- Kalibr calibration realsensed435i -- multi camera calibration
- Search Yiwu shopping (PAI Li Tao) API by image
- Object. getOwnPropertyNames() VS Object.keys()
- 按图搜索义乌购商品(拍立淘) API
- 补充—非线性规划
- Ren Zhengfei talked about the suppression of the United States again: to live is to win, and to defeat the United States
- 日本批准向韩出口EUV光刻胶,三星、SK海力士危机或将缓解
猜你喜欢

一种用于实体关系抽取的统一标签空间

Selenium automated test interview questions family bucket

Selenium自动化测试面试题全家桶

kalibr标定realsenseD435i --多相机标定

【Flutter -- GetX】弹框 - Dialog、Snackbar、BottomSheet

Supplement - nonlinear programming

Type assertion in typescript

七、微信小程序运行报错:Error: AppID 不合法,invalid appid

FreeRTOS personal notes - Software Timer

Attack and defense world ----- ics-07
随机推荐
Selenium自动化测试面试题全家桶
Preview image before uploading
Thorough load balancing
JDBC总结
获取文本选择的方向
工业基础类IFC—提取模型结构树
七、微信小程序运行报错:Error: AppID 不合法,invalid appid
Pytoch uses RNN model to build person name classifier
《暑假每日一题》Week 7:7.18 - 7.24
js点击图片打印图像
新来个技术总监要我做一个 IP 属地功能~
我的sql没问题为什么还是这么慢|MySQL加锁规则
I successfully landed the automatic testing post, with a maximum monthly salary of 15.4k. I'm great~
Also on Data Governance
NPM, NPM Chinese documents, NPM learning and using
[mysql]substr usage - query the value of specific digits of a field in the table
Content management tools, blue bookmarks are enough
从手动测试,到自动化测试老司机,只用了几个月,我的薪资翻了一倍
Kalibr calibration realsensed435i -- multi camera calibration
Object. getOwnPropertyNames() VS Object.keys()