Java中equals()方法与"=="的区别

概述

Java中equals()方法和”==”的用法在求职中经常遇到,学习之后时间长了容易忘记,所以整理一下学习笔记以便日后复习。若有不对,欢迎指正,大家相互学习。

“==”用法

1. 基本数据类型

Java基本类型分别为:byte、short、char、int、long、float、double、boolean。用”==”基本数据类型进行比较时,比较的是它们的值是否相同。例如:

1
2
3
4
5
6
7
8
int a = 10;
int b = 10;
int c = 20;
char d = 'a';
char e = 'a';
System.out.println(a == b); // true
System.out.println(a == c); // false
System.out.println(d == e); // true

注意:对于基本数据类型而言,没有equals()方法。

2. 引用类型

用于引用类型时,比较的是两个引用变量在内存中存放的地址是否相同,new出来的两个对象地址相同返回true,否则返回false。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class HelloWorld {

public static void main(String[] args) {
Hello hello1 = new Hello();
Hello hello2 = hello1;
Hello hello3 = new Hello();
System.out.println(hello1 == hello2); // hello2 是 hello1 的引用,同指向一个内存空间。返回true
System.out.println(hello1 == hello3); // hello1 和 hello3 都是 new 出来的对象,指向不同的内存空间。返回false
}
}
class Hello {
private String hello;

public Hello() {
this.hello = "你好";
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
}

equals()方法

equals方法是基类Object中的方法,所以对于所有继承于Object类都会有该方法。Object类中equals()方法的源码如下:

1
2
3
4
//Object类中的equals方法
public boolean equals(Object obj) {
return (this == obj); // 比较两个引用对象是否相等,即是否指向同一个对象
}

默认情况下,当使用equals()方法对两个对象进行比较时,比较的是两个对象的地址(此时与”==”用法相同)。测试如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class HelloWorld {
public static void main(String[] args) {
Hello hello1 = new Hello();
Hello hello2 = hello1;
Hello hello3 = new Hello();
System.out.println(hello1.equals(hello2)); // 两个对象地址相同,返回true
System.out.println(hello1.equals(hello3)); // 两个对象地址不同,返回false
}
}
class Hello {
private String hello;

public Hello() {
this.hello = "你好";
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
}

对于我们自己写的类,想要通过equal()方法比较两个对象的内容,就要重写equals()方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class HelloWorld {

public static void main(String[] args) {
Hello hello1 = new Hello();
Hello hello2 = hello1;
Hello hello3 = new Hello();
System.out.println(hello1.equals(hello2)); // 所指向的对象内容一样,返回true
System.out.println(hello1.equals(hello3)); // 内容一样,返回true
System.out.println(hello1 == hello3); // 比较两个对象的地址,地址不一样,返回false
}
}
class Hello {
private String hello;

public Hello() {
this.hello = "你好";
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
// 重写Object类的equals()方法
@Override
public boolean equals(Object obj) {
if (this == obj) { // 传入的对象就是自己,肯定时相等的
return true;
}
if (obj == null) { // 传入的对象为空,不相等
return false;
}
if (getClass() != obj.getClass()) { // 判断是否为同一类型的,如果时Hello类和其他类,就不用比较了
return false;
}
Hello other = (Hello)obj;
if (hello == null) {
if (other.hello != null) {
return false;
}
} else if (!hello.equals(other.hello)) { // 若hello属性相等,就相等
return false;
}
return true;
}
}

String类中的equals()方法

下面时String类中equals()方法的具体实现,重写之后用来比较指向的字符串对象所存储的字符串是否相等:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// String类中的equals方法
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}

String类中重写了equals()方法,调用equals()方法比较两个String对象时,比较的是String的内容。还有一些类如Double,Date,Integer等,都对equals()方法进行了重写,用来比较两个对象所存储的内容是否相等。就拿String类来举例,如下:

1
2
3
4
5
6
7
8
9
10
11
12
public static void main(String[] args) {

String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");

System.out.println(s1.equals(s2)); // 两个对象所存储的内容相等,返回true
System.out.println(s1 == s2); // 两个对象所存储的内容相等,返回true

System.out.println(s1.equals(s3)); // 两个对象所存储的内容相等,返回true
System.out.println(s1 == s3); // "=="比较两个对象的地址,两个对象地址不一样,返回false
}

总结

  1. 使用“==”时,当时基本数据类型比较时,比较它们存储的值是否相等。当比较引用类型时,比较引用变量所指向的对象地址是否相等。

  2. 使用equals() 方法时,但equals方法不能作用于基本数据类型。如果没有对equals方法进行重写,比较的是引用变量所指向的对象地址是否相等。像String类、Integer类等对equals方法进行了重写,比较的是指向对象的内容是否相等。