当前位置:网站首页>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
边栏推荐
- MySQL of database -- basic common query commands
- KVM installation and deployment
- web页面如何渲染
- MySQL - Chapter 1 (data types in MySQL)
- Trc20 fund collection solution based on thinkphp5 version
- Colorui solves the problem of blocking content in bottom navigation
- Leetcode0587. 安装栅栏(difficult)
- 《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
- MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)
- 基于ThinkPHP5版本TRC20-资金归集解决方案
猜你喜欢

Using sqlmap injection to obtain the account and password of the website administrator

机器学习(六)——贝叶斯分类器

成功的DevOps Leader 应该清楚的3个挑战
![Buuctf [actf2020 freshman competition] include1](/img/47/b8f46037f7e9476b8e01e8d6a7857a.png)
Buuctf [actf2020 freshman competition] include1
![[geek challenge 2019] havefun1](/img/8b/b15bf31771d54db25f24d630e64093.png)
[geek challenge 2019] havefun1

Project upload part

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

Chapter VIII project stakeholder management of information system project manager summary

小程序报错:Cannot read property 'currentTarget' of undefined

Leetcode-199 - right view of binary tree
随机推荐
EmuElec 编译总结
npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’
Group Backpack
[Luke V0] verification environment 2 - Verification Environment components
NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
JS case to find the maximum value, reverse the array, bubble sort
Open services in the bottom bar of idea
kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core
亚马逊云科技入门资源中心,从0到1轻松上云
Matlab draw five-star red flag
SAP 101K 411k inventory change
Employee probation application (Luzhou Laojiao)
Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
Base de la technologie électronique numérique 3.1 aperçu du circuit de porte, 3.2 circuit de porte à diode semi - conductrice
Principle of synchronized implementation
Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
The most concerned occupations after 00: civil servants ranked second. What was the first?
Go language learning notes - array | go language from scratch
Number of islands
Project upload part