Java Foundation-equals Method

Source: Internet
Author: User

The Equals side of the object class is used to determine that an object is equal to another object, as to the condition that equals, for example, equals of the string class is the same as the content of the strings must be the same, the value returned by the Equals method is true. So in our own defined class, the override of equals is common! Here are the main features of equals and the correct wording of equals, as for the exact meaning of the Equals method is not introduced here!

1. Give an example

In this introduction to the other, let's first look at the correct wording

public class Animal {    private String name = null;    public Animal(String name) {        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public boolean equals(Object obj) {        if(this == obj) {            return true;        }        if(obj == null) {            return false;        }        if(this.getClass() != obj.getClass()) {            return false;        }        Animal animal = (Animal) obj;        //return this.name.equals(animal.name);        //这个方法只在JDK7及其以后才有的        //这个方法能够保证两个name其中只有一个为null的话,返回的false        //如果两个都为null的话,返回的是true        //如果两个都不为null的话,具体看情况        return Objects.equals(this.name, animal.name);    }}

As we can see from the code above, the Equals method of the animal class determines whether the name is the same as the equality condition. To tell the truth, the conditions of judgment are very simple, but our code is very complex to write. Someone might write this:

    public boolean equals(Object obj) {        if(! (obj instanceof Animal)) {            return false;        }        Animal animal = (Animal) obj;        return this.name.equals(animal.name);    }

In fact, the above code is a big problem, as to what the problem, later! Here we explain the code written by the correct Equals method:

1. This = = obj, there is no doubt that if the memory of two objects is the same, then it must be the same object. 2. This = = null, in the same case, if the object passed in is null, it must be false. 3. This.getclass = = Obj.getclass, this condition may be a bit confusing. Let me explain, GetClass is the class object that obtains the current object, as for what is the class object, which is not explained here, it is necessary to remember that all objects of the same class have the same class object, that is, This is true if obj does not belong to the same class, then it must be false.

So this is good, so I put in the listing of a class code again!

public class Dog extends Animal{    private int age = 0;    public Dog(String name, int age) {        super(name);        this.age = age;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }        @Override    public boolean equals(Object obj) {        if(!super.equals(obj)) {            return false;        }        Dog dog = (Dog) obj;        return this.age == dog.age;    }}

We found that the dog class inherits from the animal class and overrides the Equals method of the parent class. In the dog class Equals method, we first determine whether the two objects are equal, and then compare the age is the same. This method has several benefits by calling the equals of the parent class to judge and then judging the conditions of the subclass itself:

1. Inheritance is consistent, and it is possible to judge the condition of a subclass only if they are by equals of the parent class, because if the equals of the parent class is not judged, the equals of the parent class returns false, but the equals of the child class is returned true. This does not conform to the object-oriented nature, the actual point, if the two sons and dad are not the same, how can a son the same! 2. It is avoided that obj is a subclass of the dog class (although there is no subclass of the dog Class). Because here, we first call the parent class equals, the parent class in the Equals method: This.getclass () = = Obj.getclass will help us determine whether these two classes are the same class. According to our previous notation, it is very problematic to use the INSTANCEOF keyword to judge if this is the object of the dog class, but the actual type of obj is the object of the dog subclass, and if you use obj instanceof the return of the dog is definitely true, If we pass our judgment, we end up judging our age. In fact, these two objects are certainly not the same object, so it is problematic to judge by age. As for the detailed explanation of this question, we will mention it later! 2. Properties of the Equals method

There are some foreshadowing here, which will be explained in detail, but before we explain it, let's look at the characteristics of the Equals method:

1. Reflexivity: For any non-empty object, the X.equals (x) return must be true. 2. Symmetry: For any two non-null objects x, Y, if X.equals (y) returns True, then Y.equals (x) returns is certainly true. 3. transitivity: For any three non-null objects x, Y, Z, if X.equals (y) is true, and Y.equals (z), then x.equals (z) must also be true. 4. Consistency: If object X and Object y do not have any transformations, repeated calls to Y.equals (x) should return the same result. 5. X,x.equals (NULL) for any non-null object should return FALSE.

For these features, let's take a feature to explain why using the instanceof keyword to make judgments is a big problem.
Suppose, remember here is the assumption: if the Equals method of the animal class and the Equals method of the dog class use the instanceof keyword, that is the following code:

The Equals method of the animal class:
    @Override    public boolean equals(Object obj) {        if(!(obj instanceof Animal)) {            return false;        }        Animal animal = (Animal) obj;        return Objects.equals(this.name, animal.name);    }
The Equals method of the dog class:
    @Override    public boolean equals(Object obj) {        if (!(obj instanceof Dog)) {            return false;        }        Dog dog = (Dog) obj;        return this.age == dog.age;    }
Then we write this in the Main method:
    public static void main(String[] args) {        Animal a1 = new Animal("pby");        Dog d1 = new Dog("pby", 21);        System.out.println(a1.equals(d1));        System.out.println(d1.equals(a1));    }

We can see that the first result returns True, but the second one returns false. This is problematic and does not conform to the symmetry of the Equals method.
Let's analyze when A1.equals (D1), called the Equals method in the Animal class, this method evaluates name, first D1 instanceof Animal must be true, because the dog class is a subclass of Animal, So the if condition does not block out D1, because the name of two objects is the same, so the return value is true, but the real result is false, because they do not belong to the same class! As for the second why is false, here will not be explained!
Then we come back to see the correct wording, This.getclass = = Obj.getclass This judgment will be able to shield our D1 off!

Java Foundation-equals Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.