当前位置:网站首页>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 .
边栏推荐
- arduino+水位传感器+led显示+蜂鸣器报警
- 1.C语言矩阵加减法
- View UI Plus 發布 1.3.1 版本,增强 TypeScript 使用體驗
- Leetcode.3 无重复字符的最长子串——超过100%的解法
- C语言实现扫雷游戏(完整版)
- 6. Function recursion
- [the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
- 六种集合的遍历方式总结(List Set Map Queue Deque Stack)
- Smart classroom solution and mobile teaching concept description
- View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
猜你喜欢
随机推荐
(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
FileInputStream和BufferedInputStream的比较
3.C语言用代数余子式计算行列式
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
View UI plus released version 1.3.0, adding space and $imagepreview components
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
[hand tearing code] single case mode and producer / consumer mode
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
一段用蜂鸣器编的音乐(成都)
IPv6 experiment
Arduino+ water level sensor +led display + buzzer alarm
仿牛客技术博客项目常见问题及解答(三)
FAQs and answers to the imitation Niuke technology blog project (III)
3.输入和输出函数(printf、scanf、getchar和putchar)
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
View UI Plus 发布 1.3.1 版本,增强 TypeScript 使用体验
[中国近代史] 第六章测验
仿牛客技术博客项目常见问题及解答(二)
Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript
Redis cache obsolescence strategy