当前位置:网站首页>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
边栏推荐
- Kettle experiment
- Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem
- Applet in wechat and app get current ()
- kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core
- Give the method of instantiating the object to the new object
- Vivo, hardware safe love and thunder
- Flink SQL realizes the integration of stream and batch
- Two methods of building Yum source warehouse locally
- Kettle实验 (三)
- Redis Desktop Manager for Mac
猜你喜欢

Principle of synchronized implementation

DVWA range practice

Go language learning notes - slice, map | go language from scratch

Node installation

Kettle实验 (三)

DJ music management software pioneer DJ rekordbox

阿里云架构师解读四大主流游戏架构

《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路

Go language learning notes - structure | go language from scratch

Data visualization: use Excel to make radar chart
随机推荐
112. 路径总和
501. 二叉搜索树中的众数
[in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)
Leetcode-199 - right view of binary tree
ATSS(CVPR2020)
108. 将有序数组转换为二叉搜索树
How to read excel table to database
Kettle实验 (三)
Kettle experiment
Chapter VIII project stakeholder management of information system project manager summary
108. Convert an ordered array into a binary search tree
MySQL - Chapter 1 (data type 2)
Kettle实验 转换案例
SQL used query statements
Go language learning notes - slice, map | go language from scratch
js 原型链的深入
Applet error: cannot read property'currenttarget'of undefined
【SQL server速成之路】数据库的视图和游标
Operation not allowed for a result set of type resultset TYPE_ FORWARD_ ONLY. Explain in detail
DVWA range practice record