The Equals method in the object class is used to detect whether an object is equal to another object. In the object class, this method will determine whether two objects have the same reference. If two objects have the same reference, they must be equal. From this point of view, it is reasonable to use it as a default action. For most classes, however, this judgment has little meaning. For example, it is completely meaningless to compare the equality of two PrintStream objects in this way. However, it is often necessary to detect the equality of two object states, and if the state of two objects is equal, the two objects are considered equal.
For example, if the names, salaries, and hire dates of the two employee objects are the same, they are considered equal (the comparison ID is more meaningful in the actual employee database, and the following example demonstrates the implementation mechanism of the Equals method).
Public Booleanequals (Object otherobject) {Employee other=(Employee) otherobject; //a quick test to see if the objects is identical if( This= = Otherobject)return true; //must reture False if the explicit parameter is null if(Otherobject = =NULL)return false; //If the classes don ' t match, they can ' t be equal if(GetClass ()! =Otherobject.getclass ())return false; //test whether The fields has identical values returnname.equals (other.name)&& salary==other.salary&&hireday.equals (Other.hireday); }
The GetClass method returns the class to which an object belongs. In detection, it is possible to be equal only if two objects belong to the same class.
The Java language specification requires the Equals method to have the following characteristics:
1) Reflexivity: For any non-null reference x, X.equals (x) should return true.
2) Symmetry: For any reference to x and Y, it should also return true if and only if Y.equals (x) returns True,x.equals (Y).
3) transitivity: For any reference to X, Y and Z, if X.equals (y) returns True,y.equals (z) returns True,x.equals (Z) should also return true.
4) Consistency: If the object referenced by x and Y does not change, repeated calls to X.equals (Y) should return the same result.
5) for any non-null reference x, x.equals (null) should return FALSE.
Here are some suggestions for writing the perfect Equals method:
1. The explicit parameter is named Otherobject, which later needs to be converted to another variable called other.
2. Detect if this and otherobject refer to the same object:
if (this = = Otherobject) return true;
This statement is just an optimization. In fact, this is a frequently used form. Because it is much less expensive to calculate this equation than to compare the fields in a class by one.
3. Detects if Otherobject is null, and returns FALSE if null. This test is necessary.
if (Otherobject = = null) return false;
4. Compare if this and otherobject belong to the same class. If the semantics of equals are changed in each subclass, use GetClass detection:
if (GetClass ()!=otherobject.getclass ()) return false;
If all subclasses have uniform semantics, use instanceof detection:
if (! ( Otherobject instanceof ClassName)) return false;
5. Convert Otherobject to the corresponding class-type variable:
ClassName other = (ClassName) otherobject;
6. Now it's time to compare all the fields that need to be compared. Use = = to compare the base type fields, using equals to compare object fields. Returns True if all fields match, otherwise false.
return field1 = = Other.field1 && object.equals (field2, other,field2) && ....
If you redefine equals in a subclass, you must include the call Super.equals (other) in it.
Java rules for determining whether two objects are equal