Talking about the equals method in Java

Characteristics of equals method in java specification

  • Reflexivity (for any non-null reference x, x.equals(x) returns true;)

  • Symmetry (for any reference x, y, if and only if y.equals(x) returns true, x.equals(y) returns true;)

  • Transitivity (for any reference x, y, z, if x.equals(y) returns true, y.equals(z) returns true; then x.equals(z) returns true;)

  • Consistency (if the objects referenced by x and y have not changed, calling x.equals(y) repeatedly should return the same result.)

  • For any non-null reference x, x.equals(null) returns false;

Master the usage of equals through the following example

package org.java.base.equals;

public class TestEquals {
public static void main(String[] args) {
/**
* The construction method Cat() is used here to create two new cats in the heap memory.
* The color, weight and height of these two cats are all The same,
* but c1 and c2 will never be equal. This is because c1 and c2 are reference objects of two cats in the heap memory respectively, and
* contains the addresses where the two cats can be found, but because the two cats are in The heap memory is stored in two different spaces,
* so c1 and c2 are respectively loaded with different addresses, so c1 and c2 will never be equal.
*/
Cat c1 = new Cat(1, 1, 1);
Cat c2 = new Cat(1, 1, 1);
System.out.println("The result of c1==c2 is:"+(c1==c2 ));//false
System.out.println("The result of c1.equals(c2) is:"+c1.equals(c2));//false
}
}

class Cat {
int color, weight, height;
/**
* 定义一只猫
* @param color–颜色
* @param weight–重量
* @param height–高度
*/
public Cat(int color, int weight, int height) {
this.color = color;
this.weight = weight;
this.height = height;
}
}

The output is as follows:

The result of c1==c2 is: false The result of
c1.equals(c2) is: false

 c1 points to an object, c2 also points to an object, c1 and c2 contain the addresses stored in the heap memory of the two Cat objects. Since the two Cat objects are located in different storage spaces, c1 and c2 are contained The addresses of are definitely not equal, so the two reference objects c1 and c2 are definitely not equal. So execute: "System.out.println(c1==c2);" The printed result must be false. So you have two new objects. Don't worry, the references of these two objects will never be the same. If they are the same, one of them will be overwritten. This is not true. Whether c1 is equal to c2 is the content contained in the two references of c1 and c2, because the references of the two new objects will never be the same, so the content of the two references of c1 and c2 will never be the same. Therefore, c1 can never be equal to c2. Therefore, by comparing the references of two objects, it is never possible to make the two objects equal, exactly the same.

  要想判断两个对象是否相等,不能通过比较两个对象的引用是否相等,这是永远都得不到相等的结果的,因为两个对象的引用永远不会相等,所以正确的比较方法是直接比较这两个对象,比较这两个对象的实质是不是一样的,即这两个对象里面的内容是不是相同的,通过比较这两个对象的属性值是否相同而决定这两个对象是否相等。

  Object类提供了一个equals()方法来比较两个对象的内容是否相同,因此我们可以采用这个方法去比较两个对象是否在逻辑上“相等”。如:c1.equals(c2);这里是调用从Object类继承下来的equals()方法,通过查阅API文档得到Object类里的equals方法的定义如下:

public boolean equals(Object obj)

  在Object这个类里面提供的Equals()方法默认的实现是比较当前对象的引用和你要比较的那个引用它们指向的是否是同一个对象,即和“c1==c2”这种写法是一样的,“c1.equals(c2)”与“c1==c2”是完全等价的。因此直接使用继承下来的equals()方法也是无法直接比较两个对象的内容是否相同的,为此,我们必须得重写equals()方法,改变这个方法默认的实现。

public boolean equals(Object obj){
if (obj==null){
return false;
}
else{
/**
* instanceof是对象运算符。
* 对象运算符用来测定一个对象是否属于某个指定类或指定的子类的实例。
* 对象运算符是一个组合单词instanceof。
* 该运算符是一个双目运算符,其左边的表达式是一个对象,右边的表达式是一个类,
* 如果左边的对象是右边的类创建的对象,则运算结果为true,否则为false。
*/
if (obj instanceof Cat){
Cat c = (Cat)obj;
if (c.color==this.color && c.weight==this.weight && c.height==this.height){
return true;
}
}
}
return false;
}

这一次得到的结果就与上次没有重写equals()方法时得到的结果就不一样了:

c1==c2的结果是:false
c1.equals(c2)的结果是:true

  “System.out.println(c1 == c2);”打印出来的结果依然是false,因为这里是比较两个对象的引用里面的内容,这两个引用里面的内容当然不相等,而且永远不会相等,所以打印出来的结果肯定是false。

  “System.out.println(c1.equals(c2));”打印出来的结果为true,因为我们在Cat类里面重写了equals()方法,改变了这个方法默认的实现,我们把方法的实现改为只要这个两个对象是真的存在,并且都是猫,并且它们的颜色(color),身高(height)和体重(weight)都相同,那么这两只猫在逻辑上就是一模一样的,是完全相同的两只猫,即这两只猫是“相等”的。所以这里打印出来的结果是true。

1.3.如何比较两个字符串对象是否相等?

图片

在String类里面是这样重写equals()方法的实现的:用当前的这个字符串对象和指定的字符串对象比较,指定的字符串对象不能为空并且这个对象的字符序列和当前这个字符串对象的字符串序列一样,如果这些条件都满足,那么这两个字符串对象就是相等的。

总结:比较两个对象是否相等,我们采用equals()方法,判断两个对象是否相等的条件是由我们重写equals()方法的实现后定义的,这样就可以比较灵活地使用equals()方法在不同的类里面比较位于同一类下的两个对象是否相等了。


Guess you like

Origin blog.51cto.com/15082402/2644371