当前位置:网站首页>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 .
边栏推荐
- 5. Function recursion exercise
- [the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
- [中国近代史] 第六章测验
- 优先队列PriorityQueue (大根堆/小根堆/TopK问题)
- 6.函数的递归
- 13 power map
- Atomic and nonatomic
- 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
- 【九阳神功】2019复旦大学应用统计真题+解析
- C语言入门指南
猜你喜欢
Write a program to simulate the traffic lights in real life.
3.输入和输出函数(printf、scanf、getchar和putchar)
C language Getting Started Guide
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
FAQs and answers to the imitation Niuke technology blog project (III)
Smart classroom solution and mobile teaching concept description
9.指针(上)
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
13 power map
2. C language matrix multiplication
随机推荐
C language Getting Started Guide
自定义RPC项目——常见问题及详解(注册中心)
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
ABA问题遇到过吗,详细说以下,如何避免ABA问题
仿牛客技术博客项目常见问题及解答(二)
【九阳神功】2019复旦大学应用统计真题+解析
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
Implement queue with stack
Custom RPC project - frequently asked questions and explanations (Registration Center)
(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
3.C语言用代数余子式计算行列式
Data manipulation language (DML)
Comparison between FileInputStream and bufferedinputstream
[中国近代史] 第九章测验
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
C语言实现扫雷游戏(完整版)
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
C语言入门指南
【毕业季·进击的技术er】再见了,我的学生时代