How does equals() method work in Java [duplicate]

2019-01-26 14:25发布

问题:

This question already has an answer here:

  • What is the difference between == and equals() in Java? 22 answers

The equals method compares whether two object values are equal or not. My question is how it compares the two objects? How can it tell the two objects are equal or not? I want to know based on what it compares the two objects. I am not including the hashCode method.

回答1:

The default implementation, the one of the class java.lang.Object, simply tests the references are to the same object :

150    public boolean equals(Object obj) {
151        return (this == obj);
152    }

The reference equality operator is described like this in the Java Specification :

At run time, the result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false.

This default behavior isn't usually semantically satisfying. For example you can't test equality of big Integer instances using == :

Integer a = new Integer(1000);
Integer b = new Integer(1000);
System.out.println(a==b); // prints false

That's why the method is overridden :

722     public boolean equals(Object obj) {
723         if (obj instanceof Integer) {
724             return value == ((Integer)obj).intValue();
725         }
726         return false;
727     }

which enables this :

System.out.println(a.equals(b)); // prints true

Classes overriding the default behavior should test for semantic equality, based on the equality of identifying fields (usually all of them).

As you seem to know, you should override the hashCode method accordingly.



回答2:

Consider following example,

public class Employee {
    String name;
    String passportNumber;
    String socialSecurityNumber;
    public static void main(String[] args) {
        Employee e1 = new Employee();
        Employee e2 = new Employee();
        boolean isEqual = e1.equals(e2);  // 1
        System.out.println(isEqual);
    }
}

In the code at comment //1 it calls inherited equals method from Object class which is simply comparing references of e1 and e2. So it will always give false for each object created by using new keyword.

Following is the method excerpt from Object

public boolean equals(Object obj) {
    return (this == obj);
}

For comparing equality check JLS has given equals method to override in our class. It is not final method. JLS doesn't know on what basis programmar wants to make two objects equal. So they gave non-final method to override.
hashcode does not play role to check object's equality. hashcode checks/finds the Bucket where object is available. we use hashcode in hashing technique which is used by some classes like HashMap..

If two object's hashcode are equals that doesn't means two objects are equal.
For two objects, if equals method returns true then hashcode must be same.

You will have to override equals method to decide on which basis you want object e1 and e2 in above code is equal. Is it on the basis of passportNumber or socialSecurityNumber or the combination of passportNumber+socialSecurityNumber?

I want to know based on what it compares the two objects.

Answer is, by default with the help of inherited Object class's equals method it compares two object's reference equality by using == symbol. Code is given above.



回答3:

logically, equals does not compare objects (however you can do anything with it), it compares values. for object comparison there is '==' operator