当前位置:网站首页>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
边栏推荐
- 3、 6 [Verilog HDL] gate level modeling of basic knowledge
- Canary publishing using ingress
- npm ERR! network
- [geek challenge 2019] havefun1
- JS case to find the maximum value, reverse the array, bubble sort
- ALV树(LL LR RL RR)插入删除
- Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
- 三、6【Verilog HDL】基础知识之门级建模
- DVWA range practice record
- 考研线性代数常见概念、问题总结
猜你喜欢

搞不懂时间、时间戳、时区,快来看这篇

Failed to download esp32 program, prompting timeout

kernel-pwn学习(4)--Double Fetch&&0CTF2018-baby

Summary of wrong questions 1

小程序报错 :should have url attribute when using navigateTo, redirectTo or switchTab

MySQL of database -- basic common query commands

501. 二叉搜索树中的众数

小程序报错:Cannot read property 'currentTarget' of undefined
![3、 6 [Verilog HDL] gate level modeling of basic knowledge](/img/36/46f2413ecb12f81c003848c93f6bc9.jpg)
3、 6 [Verilog HDL] gate level modeling of basic knowledge

Matlab draw five-star red flag
随机推荐
kettle庖丁解牛第14篇之JSON输入
Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
Installation of data cleaning ETL tool kettle
What is augmented reality technology? Where can it be used?
JS case to find the maximum value, reverse the array, bubble sort
Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
ALV树(LL LR RL RR)插入删除
《信息系统项目管理师总结》第八章 项目干系人管理
1 + X cloud computing intermediate -- script construction, read-write separation
Single sign on SSO
阿里云架构师解读四大主流游戏架构
npm ERR! network
Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
501. Mode in binary search tree
高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
MySQL of database -- overview and installation
Sql1 [geek challenge 2019]
Two methods of building Yum source warehouse locally
LeetCode 1611. The minimum number of operations to make an integer 0
Get trustedinstaller permission