当前位置:网站首页>String abc = new String(“abc“),到底创建了几个对象
String abc = new String(“abc“),到底创建了几个对象
2022-07-06 09:20:00 【快醒醒鸭今天你编程了吗?】
String abc = new String("abc");
咱们先不着急回答这个到底创建了几个人对象,先分析:
首先这个代码里面有一个‘new’关键字,我们大概都听过凡是new出来的都是放在堆中的,这个关键字是在程序运行的时候,根据已经加载的系统类String在堆里面实例化一个字符串对象,然后在这个String的构造方法里面传递了一个“abc”的字符串,因为String里面的字符串成员变量底层是用final修饰的,所以它是一个字符串常量,所以JVM会拿字面量“abc”去字符串常量池里面去找,对应它的一个String的对象引用,如果拿不到就会在堆内存里面去创建一个“abc”的String对象,并且把引用保存到字符串常量池里面,并且把引用保存到字符串常量池里面,以后如果再有字面量“abc”的定义,因为字符串常量池里面,已经存在字面量“abc”的一个引用,所以只需要从常量池中获取对应的引用就可以了不需要在去创建。
对于这个问题我认为有两个
- 如果”abc“这个字符串常量不存在,则需要去创建两个对象,分别是‘abc’这个字符串常量,以及‘new String’这个实例对象
- 如果‘abc’这字符串常量存在,则只会创建一个对象,就是String这个对象。
以上就是这个问题的全部解释,主要考察的对JVM里面运行时内存划分和JVM常量池的理解足够深刻才能回答出。
边栏推荐
- 学编程的八大电脑操作,总有一款你不会
- Abstract classes and interfaces
- Interview Essentials: talk about the various implementations of distributed locks!
- C语言实现扫雷游戏(完整版)
- 7. Relationship between array, pointer and array
- View UI plus releases version 1.1.0, supports SSR, supports nuxt, and adds TS declaration files
- 【九阳神功】2018复旦大学应用统计真题+解析
- 13 power map
- 1.C语言矩阵加减法
- Cloud native trend in 2022
猜你喜欢
System design learning (I) design pastebin com (or Bit.ly)
Application architecture of large live broadcast platform
西安电子科技大学22学年上学期《信号与系统》试题及答案
arduino+水位传感器+led显示+蜂鸣器报警
Change vs theme and set background picture
String class
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
TYUT太原理工大学2022数据库大题之分解关系模式
TYUT太原理工大学2022软工导论大题汇总
Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
随机推荐
Implement queue with stack
The latest tank battle 2022 - full development notes-3
TYUT太原理工大学2022数据库大题之分解关系模式
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
View UI Plus 发布 1.3.1 版本,增强 TypeScript 使用体验
1.C语言矩阵加减法
View UI plus released version 1.3.0, adding space and $imagepreview components
8.C语言——位操作符与位移操作符
7. Relationship between array, pointer and array
Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
初识C语言(上)
最新坦克大战2022-全程开发笔记-2
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
165. Compare version number - string
C language to achieve mine sweeping game (full version)
Tyut Taiyuan University of technology 2022 introduction to software engineering summary
Branch and loop statements
TYUT太原理工大学2022软工导论大题汇总
MySQL Database Constraints
9.指针(上)