当前位置:网站首页>简单理解==和equals,String为什么可以不用new
简单理解==和equals,String为什么可以不用new
2022-04-23 06:25:00 【小白蛋挞】
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();
System.out.println(car1==car2);
System.out.println(car1.equals(car2));
String a = "小";
String b = "小";
System.out.println(a==b);
System.out.println(a.equals(b));
}
false
false
true
true
==比较的是内存地址,equals是对字符串内容的比较。
实体类型比较里面的内容需要重写equals和hashcode()方法。
String为什么可以不用new?
因为jvm中存在一个字符串常量池
String a ="abc";首先jvm会去常量池中看有没有"abc"这个字符串,如果有则指向它,没有则在常量池中开辟一块空间存放abc
String a = new String("aba")这个语句有可能创建了一个对象,也有可能创建了两个对象。
创建1一个对象的情况,常量池中已经有了字符串aba,因为有了new那么只需要在堆中创建一个String类型的对象,然后a指向常量池中的aba
创建两个对象的情况,除了new出来的一个对象外,还要在常量池中创建一个对象存储aba,然后在进行赋值操作,将abn赋值一份到堆中。
版权声明
本文为[小白蛋挞]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_61547956/article/details/124218915
边栏推荐
- 反思|开启B站少女心模式,探究APP换肤机制的设计与实现
- Object. Create() principle, object Create() specification, handwritten object create(),Object. Create() usage
- 技能点挖坑
- 积性函数前缀和——杜教筛
- 13. User and authority management
- 菜菜的并发编程笔记 |(九)异步IO实现并发爬虫加速
- 2. Restricted query
- OpenGL超级宝典初步配置(freeglut、glew、gltools、glut)
- Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
- 超级宝典&编程指南(红蓝宝书)-读书笔记
猜你喜欢

Nacos / sentinel gateway current limiting and grouping (code)

SAP Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。

SAP PI/PO Soap2Proxy 消费外部ws示例

Authorization+Token+JWT

Mysql持久性的实现

keytool: command not found

Authorization server (simple construction of authorization server)

BTree、B+Tree和HASH索引

Background management system framework, there is always what you want

超级宝典&编程指南(红蓝宝书)-读书笔记
随机推荐
关于素数的不到100个秘密
数论之阶与原根讲解
(扩展)BSGS与高次同余方程
莫比乌斯反演
Object. Create() principle, object Create() specification, handwritten object create(),Object. Create() usage
7.子查询
如何判断点是否在多边形内(包含复杂多边形或者多边形数量很多的情况)
Django使用mysql数据库报错解决
[Ted series] how to get along with inner critics?
学会使用搜索引擎
配置npm
Implementation of MySQL persistence
[self motivation series] you'll never be ready
手游性能优化
SAP pi / PO rfc2restful publishing RFC interface is a restful example (proxy indirect method)
公共依赖模块common的处理
ogldev-读书笔记
Visualization Road (IX) detailed explanation of arrow class
Discussion on arrow function of ES6
菜菜的并发编程笔记 |(九)异步IO实现并发爬虫加速