当前位置:网站首页>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
边栏推荐
- 关于模板函数声明与定义的问题[通俗易懂]
- Network related journals and conferences in CS
- Vs2019 configuring opencv
- Study notes of the first week of sophomore year
- RecyclerView最后一条显示不全或显示部分的问题解决
- 【Halcon视觉】图像灰度变化
- Self encapsulated database dbutils universal template
- 畅听,网文流量竞争的下一站?
- Dynamically determine file types through links
- 数据库的复习--1.概述
猜你喜欢

Dynamically determine file types through links

Review of database -- 1. Overview

【Halcon视觉】图像的傅里叶变换

Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component

Beginner of flask framework-04-flask blueprint and code separation

Learning about opencv (1)

【Halcon视觉】编程逻辑

Data communication foundation TCPIP reference model
![[qualcomm][network] QTI service analysis](/img/76/49054ff8c7215eca98cc479ab1d986.png)
[qualcomm][network] QTI service analysis

【Halcon视觉】软件编程思路
随机推荐
Flask framework beginner-03-template
【C#语言】LINQ概述
Formwork (III)
Learning about opencv (4)
【Halcon视觉】仿射变换
Use of Android grendao database
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
The problem of incomplete or partial display of the last recyclerview is solved
Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
Under win10 64 bit, matlab fails to configure notebook
万字详解“用知识图谱驱动企业业绩增长”
RecyclerView最后一条显示不全或显示部分的问题解决
Okaleido生态核心权益OKA,尽在聚变Mining模式
[qualcomm][network] QTI service analysis
Basic usage of protobuf
[Qualcomm][Network] qti服务分析
C language course design Tetris (Part 1)
在.NET 6.0中配置WebHostBuilder
数通基础-TCPIP参考模型
【Halcon视觉】极坐标变换