当前位置:网站首页>String ABC = new string ("ABC"), how many objects are created
String ABC = new string ("ABC"), how many objects are created
2022-07-06 13:39:00 【Wake up duck, did you program today?】
String abc = new String("abc");
Let's not worry about answering this question. How many people have been created , First analysis :
First of all, there is a ‘new’ keyword , We've probably heard of everything new What comes out is put in the pile , This keyword is when the program is running , According to the loaded system classes String Instantiate a string object in the heap , And then here String The construction method of passes a “abc” String , because String The bottom layer of the string member variable inside is final Embellished , So it's a string constant , therefore JVM Can take a literal measure “abc” Go to the string constant pool to find , One corresponding to it String Object reference of , If you can't get it, you will create one in the heap memory “abc” Of String object , And save the reference to the string constant pool , And save the reference to the string constant pool , If there is any more literal quantity in the future “abc” The definition of , Because the string constant pool , Literal quantity already exists “abc” A reference to , So you only need to get the corresponding reference from the constant pool, and you don't need to create .
For this problem, I think there are two
- If ”abc“ This string constant does not exist , You need to create two objects , Namely ‘abc’ This string constant , as well as ‘new String’ This instance object
- If ‘abc’ This string constant exists , Then only one object will be created , Namely String This object .
The above is the full explanation of this problem , Mainly investigated right JVM There are runtime memory partitions and JVM The understanding of constant pool is deep enough to answer .
边栏推荐
猜你喜欢
8. C language - bit operator and displacement operator
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
MySQL事务及实现原理全面总结,再也不用担心面试
2.C语言初阶练习题(2)
Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
3. Number guessing game
9. Pointer (upper)
2. C language matrix multiplication
8.C语言——位操作符与位移操作符
1.C语言初阶练习题(1)
随机推荐
View UI Plus 发布 1.3.1 版本,增强 TypeScript 使用体验
PriorityQueue (large root heap / small root heap /topk problem)
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
凡人修仙学指针-1
Quickly generate illustrations
fianl、finally、finalize三者的区别
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
【九阳神功】2017复旦大学应用统计真题+解析
受检异常和非受检异常的区别和理解
ArrayList的自动扩容机制实现原理
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
7. Relationship between array, pointer and array
The latest tank battle 2022 full development notes-1
CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
View UI Plus 發布 1.3.1 版本,增强 TypeScript 使用體驗
[面试时]——我如何讲清楚TCP实现可靠传输的机制
JS interview questions (I)
用栈实现队列
FAQs and answers to the imitation Niuke technology blog project (III)
View UI plus released version 1.3.0, adding space and $imagepreview components