当前位置:网站首页>The difference between equals and = =
The difference between equals and = =
2022-07-26 10:21:00 【Haha】
Say first conclusion :
stay Java in equals And == Both are used to judge whether two data are equal , And they all have their own application scenarios .
Let's start with logical operators == , For basic data types , It compares values , For reference types , It compares the memory address of the object , Judgment Two quotes Whether to point to the same object in heap memory . because Java Language only has value passing , So for == Come on , The comparison is all worth ( Only the value of the basic data type is the value itself , The value of the reference type is the object address );
equals yes Object Class member methods , Every class can override this method , If there is no override in the custom class equals Method , It will inherit Object class equals Method , Its function is similar to that of logical operators == identical .
Practical application , Custom classes often override Object class equals Method , It is generally used to compare whether the contents of two independent objects are the same ( It depends on the implementation of specific methods ).
We analyze it concretely through a simple memory model :
In the figure a And b Is the basic data type (int a = 5 ,int b = 5);
str1 And str2 by String type (String class Object Class equals Member method );
user1 And user2 For custom class objects ( There is no override in this class equals Method , Used with String The value of type is used as a reference , Verify the above conclusion )
Code implementation :
/** * Test class */
public class Demo {
public static void main(String[] args) {
int a = 5;
int b = 5;
String str1 = new String("abc");
String str2 = new String("abc");
User user1 = new User(" Zhang San ");
User user2 = new User(" Zhang San ");
// Basic data type does not equals Method
System.out.println(a == b);// Expected operating results true
//String Class overridden equals Method , Used to compare whether the contents of two independent objects are the same
System.out.println(str1 == str2);// Expected operating results false
System.out.println(str1.equals(str2));// Expected operating results true
// Custom class objects are not overridden equals( Logical operators == And equals The method works the same , The expected operation results are consistent )
System.out.println(user1 == user2);// Expected operating results false
System.out.println(user1.equals(user2));// Expected operating results false
}
}
/** * Customize User class */
class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Running results :
D:\installPath\Java\jdk1.8.0_121\bin\java.exe "-javaagent:D:\installPath\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar
true
false
true
false
false
Process finished with exit code 0
The running result is in line with the expectation
expand
Customize User Class is also overridden equals Method , Let's see whether the running results meet the expectations ( Develop a good programming habit , rewrite equals Method, also remember to rewrite hashCode Method )
Code example :
import java.util.Objects;
/** * Test class */
public class Demo {
public static void main(String[] args) {
int a = 5;
int b = 5;
String str1 = new String("abc");
String str2 = new String("abc");
User user1 = new User(" Zhang San ");
User user2 = new User(" Zhang San ");
// Basic data type does not equals Method
System.out.println(a == b);// Expected operating results true
//String Class overridden equals Method , Used to compare whether the contents of two independent objects are the same
System.out.println(str1 == str2);// Expected operating results false
System.out.println(str1.equals(str2));// Expected operating results true
// Custom class objects have also been overridden equals Method
System.out.println(user1 == user2);// Expected operating results false
System.out.println(user1.equals(user2));// Expected operating results true
}
}
/** * Customize User Class overridden equals And hashCode Method */
class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(name, user.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}
Running results :
D:\installPath\Java\jdk1.8.0_121\bin\java.exe "-javaagent:D:\installPath\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar
true
false
true
false
true
Process finished with exit code 0
The running result is in line with the expectation
边栏推荐
- 函数模板参数(函数参数在哪)
- Use spiel expressions in custom annotations to dynamically obtain method parameters or execute methods
- PTA class a 1002
- Interview shock 68: why does TCP need three handshakes?
- Rocky basic exercise -shell script 2
- 数据库的复习--1.概述
- SQL Server 2008 R2 installation problems
- Wu Enda linear regression of machine learning
- The charm of SQL optimization! From 30248s to 0.001s
- 【C#语言】具名类型和匿名类型
猜你喜欢
Learning about opencv (2)
【Halcon视觉】形态学腐蚀
【Halcon视觉】编程逻辑
数通基础-STP原理
Deduct daily question 838 of a certain day
AirTest
Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
protobuf的基本用法
[qualcomm][network] QTI service analysis
随机推荐
关于模板函数声明与定义的问题[通俗易懂]
【Halcon视觉】软件编程思路
Kaptcha image verification code integration
INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式
Learning about opencv (1)
【Halcon视觉】图像滤波
Use of pclint in vs2013
30 minutes to thoroughly understand the synchronized lock upgrade process
Review of database -- 3. SQL language
What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
关于函数模板描述错误的是(链接格式错误怎么解决)
SQL Server 2008 R2 installation problems
Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
The charm of SQL optimization! From 30248s to 0.001s
How to use Gmail to pick up / send mail on Foxmail
Draco developed by Google and Pixar supports USD format to accelerate 3D object transmission & lt; Forward & gt;
Interview shock 68: why does TCP need three handshakes?
Nacos custom service change subscription
Jpg to EPS
Learning about opencv (3)