In Java, only 8 basic types are not objects, for example: 4 Types of shaping (byte, short, Int,long), 2 floating-point types (flout, double), Boolean, char is not an object, all other types, whether an object array, Lists, and so on, extend the object class. Understanding the design principles and implementation of methods in learning object helps to better understand the Java language. Let's begin by learning about the Equals method in object. When judging the equality of two objects, the JVM first looks for the hashcode of two objects, returns False if the two hashcode are different, or, if the hashcode is the same, calls the Equals method for comparison.
The Equals method in Object
The Equals method in object is used to detect whether an object is equal to another object. Object class, determine whether two objects refer to the same object implementation of the Equals method, the source code is as follows:
PublicBooleanEquals (Object obj) {
return( This= = obj);
}
In real-world processing, to compare two objects for equality, you generally need to override the Equals method.
Example 1: Employee Object comparison
If the names of two employees are the same as the salary and employment, they are considered equal. Override the Equals method as follows:
PublicclassEmployee {
PrivateString name;
PrivateDoubleSalary
PrivateDate Hireday;
...
@Override
PublicBooleanEquals (Object obj) {
//If there are different references to the same object, the same
if( This= = obj) {
return true;
}
//returns False if the passed-in object is empty
if(obj = =NULL) {
returnfalse;
}
//if the two belong to different types, they cannot be equal
if(GetClass ()! = Obj.getclass ()) {
returnfalse;
}
//same type, compare content to same
Employee other = (employee) obj;
returnObjects.equals (name, other.name) && salary = = Other.salary && objects.equals (Hireday, other.hireday);
}
}
Note that comparisons of two objects are equal by the static function equals in objects. The source code of the method is as follows:
PublicStaticBooleanEquals (Object A, object B) {
return(A = = b) | | (A! =NULL&& A.equals (b));
}
This returns True when two objects are null, for example, the name of the two employee object is null and returns true; If the first object is not NULL, the A.equals (object obj) method is called
Object comparison and inheritance
If the implicit and explicit object types are inconsistent, what does the Equals method do? in Example 1, if the type of two objects is inconsistent, return false directly; Most programmers use instanceof detection directly:
if(! (objinstanceofEmployee))return false;
Not only does this not resolve the errors that inherit the subclass, but it can also throw some other errors, in Example 1, in the way that coercion type conversions are used.
To implement sub-class object comparisons, first look at the constraints of the Equals method in the Java specification.
Features of the Equals method in the Java specification
For any non-null reference x, X.equals (x) returns true;
For any reference to X, Y, and only if Y.equals (x) returns True, X.equals (y) returns true;
For any reference x, Y, Z, if X.equals (y) returns True, Y.equals (z) returns true; Then X.equals (z) returns true;
If the object referenced by x and Y does not change, repeated calls to X.equals (Y) should return the same result.
- For any non-null reference x, x.equals (null) returns false;
The following can be seen in two different situations:
- If a subclass can have its own concept of equality, the symmetry requirement is forced to detect with GetClass
- If the concept of equality is determined by the superclass, then it is instrumented with instanceof so that an equal comparison can be made between objects that do not use subclasses
Comments:
- Contains more than 150 implementations of the Equals method in a standard Java library, including instanceof detection, calling GetClass detection, capturing classcastexception detection, or doing nothing. In the Java.sql.TimeStamp implementation, it was noted that the timestamp class inherits the date class, while the latter's Equals method uses a instanceof detection so that when overriding the Equals method, it cannot be symmetric at the same time.
- When determining equality by the superclass, you can consider the final keyword to modify the comparison function, if you consider the subclass equals method flexibility, can not be modified, such as the Abstractset.equals method, should be declared final, This allows you to compare subclasses HashSet and TreeSet, but with the flexibility of subclasses, no adornments are added.
Suggestions for writing the Equals method:
- Display parameter named Otherobject, later converted to other variable public boolean equals (Object otherobject)
- Detects if this and otherobject are references to the same object, yes, returns true; if (this ==otherobject) {
return true;
}
- Detects if otherobject is null, yes, returns false; if (Otherobject = = null) {
return false;
}
- Compares whether 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 so the subclass semantics are the same, use instanceof detection: if (! ( Otherobject instanceof Employee)) {
return false;
}
- Convert Otherobject to the corresponding type variable otheremployee other = (Employee) otherobject;
- Compare the data fields that you want to compare. If it is a basic data type, use a==b comparison; If the object is compared, call Objects.equals (A, B) to compare the return objects.equals (name, other.name) && salary = = other.salary & amp;& objects.equals (Hireday, other.hireday);
The whole process can be referred to example 1;
Common Equals method implementation error PublicclassEmployee {
PublicBooleanEquals (Employee other) {
returnObjects.equals (name, other.name) && salary ==other.salary && objects.equals (Hireday, other.hireday);
}
The type of display parameter declared by this method is employee. The result does not overwrite the Equals method in object, but rather defines a completely unrelated method. To avoid a type error, you can use @override to mark a method that overrides a superclass.
---restore content ends---
How to override the Equals method in Java