First look at the requirements of the Java language specification for the Equals method:
1, reflexive, for any non-controlled reference x,x.equals (x) should return true.
2, symmetry, for any reference to x and Y, if X.equals (y) returns True, then Y.equals (x) should also return true.
3, transitivity, if X.equals (y), y.equals (z) all return True, then X.equals (z) returns TRUE.
4, consistency, if the object referenced by x and Y does not change, the same result is returned regardless of how many times x.equals (y) is called.
5, you should return false for any non-null reference x,x.equals (NULL).
The Equals method in the object class is used to detect whether two objects are equal, and the criterion for equality is whether they refer to the same object.
Here's the principle of writing a perfect Equals method:
1, name the object class explicit parameter of equals Otherobject.
2, detects whether Otherobject is referenced from the same object as this.
3, detects if Otherobject is null, returns False if it is.
4, to detect whether this and otherobject belong to the same class, it is important to note that when the comparison of two objects do not belong to the same class, such as P.equals (e), p is a person,e is an employee, Employee inherits from person. Person has name,age private domain, employee has occupation domain.
If the concept of equality is that all fields of an object must be equal, P.equals (e) uses e instalceof person to return true, but if E.equals (p) is called, p instanceof employee returns FALSE, The symmetry requirement is not met, so it is necessary to force the use of getclass () for type detection.
If the concept of equality is determined by the superclass, such as that the person and employee are equal if the name and age are the same, then the instance can be used for detection, and the method is defined as final in the superclass.
5, convert Otherobject to a variable of this type.
6, now start the comparison of private data fields, for basic types such as numeric values, characters and Boolean types using = = to compare, use equals for the object domain comparison, all fields are the same return true.
7, use the @override declaration to help with error checking.
Examples are as follows:
1 class Person2 {3 PrivateString name;4 Private intAge ;5 6 PublicString getName ()7 { 8 returnname;9 } Ten Public intgetage () One { A returnAge ; - } -@Override Public Booleanequals (Object otherobject) the { - if( This==otherobject) - return true; - if(Otherobject = =NULL) + return false; - if(GetClass ()! =Otherobject.getclass ()) + return false; Aperson other =(person) otherobject; at return(Name.equals (Other.name) && (age = =other.age)); - } - } - classEmployeeextends Person - { - PrivateString occupation; in -@Override Public Booleanequals (Object otherobject) to { + if( This==otherobject) - return true; the if(Otherobject = =NULL) * return false; $ if(GetClass ()! =Otherobject.getclass ())Panax Notoginseng return false; - if(!Super. Equals (otherobject)) the return false; +Employee other =(Employee) otherobject; A returnoccupation.equals (other.occupation); the } +}
Writing a perfect Equals method in Java