当前位置:网站首页>Simply understand = = and equals, why can string not use new
Simply understand = = and equals, why can string not use new
2022-04-23 09:27:00 【Small white egg tart】
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 = " Small ";
String b = " Small ";
System.out.println(a==b);
System.out.println(a.equals(b));
}
false
false
true
true
== Compare memory addresses ,equals Is a comparison of the contents of the string .
The contents of entity type comparison need to be rewritten equals and hashcode() Method .
String Why not use new?
because jvm There is a string constant pool in
String a ="abc"; First jvm I will go to the constant pool to see if there is "abc" This string , If so, point to it , If not, open up a space in the constant pool to store abc
String a = new String("aba") This statement may create an object , It is also possible to create two objects .
establish 1 The case of an object , There is already a string in the constant pool aba, Because of the new Then you just need to create one in the heap String Object of type , then a Point to in constant pool aba
Creating two objects , except new Out of an object , Also create an object store in the constant pool aba, Then perform the assignment operation , take abn Assign a copy to the heap .
版权声明
本文为[Small white egg tart]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230624113979.html
边栏推荐
- Simple understanding of arguments in JS
- MySQL小練習(僅適合初學者,非初學者勿進)
- 小女孩行走
- MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)
- KVM installation and deployment
- ALV树(LL LR RL RR)插入删除
- 653. 两数之和 IV - 输入 BST
- 《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
- 【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)
- Colorui solves the problem of blocking content in bottom navigation
猜你喜欢
Redis Desktop Manager for Mac
Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
ATSS(CVPR2020)
GoLand debug go use - white record
Using sqlmap injection to obtain the account and password of the website administrator
A must see wechat applet development guide 1 - basic knowledge
112. Path sum
npm ERR! network
Kettle实验 (三)
node安装
随机推荐
考研线性代数常见概念、问题总结
Summary of common concepts and problems of linear algebra in postgraduate entrance examination
What is augmented reality technology? Where can it be used?
Flink 流批一体在小米的实践
Pre parsing of JS
小程序报错:Cannot read property 'currentTarget' of undefined
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
MySQL小練習(僅適合初學者,非初學者勿進)
基于ThinkPHP5版本TRC20-资金归集解决方案
Machine learning (VI) -- Bayesian classifier
Node installation
Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
#yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''
DMP engine work summary (2021, lightsaber)
《信息系统项目管理师总结》第八章 项目干系人管理
npm ERR! network
SAP 101K 411k inventory change
Kettle experiment conversion case
653. 两数之和 IV - 输入 BST
ATSS(CVPR2020)