The content of this chapter mainly addresses the following issues:
What is the role of 1 equals ()?
What is the difference between 2 equals () and = =?
What is the role of 3 hashcode ()?
4 What is the connection between hashcode () and Equals ()?
Role of the 1th part equals ()
The Equals () function is used to determine whether two objects are equal.
Equals () is defined in the Object.java of the JDK. Distinguish whether two objects are equal (that is, whether they are the same object) by determining whether their addresses are equal. The source code is as follows:
public boolean equals (Object obj) {
return (this = = obj);
}
Since the Equals () method is defined in Object.java, this means that all Java classes implement the Equals () method, and all classes can compare the equality of two objects by equals (). However, as we have said, using the default Equals () method is equivalent to the "= =" method. Therefore, we typically override the Equals () method: If the contents of two objects are equal, the Equals () method returns true; otherwise, returns FASLE.
The following is divided into 2 classes, based on whether the class overrides Equals () method.
(01) If a class does not overwrite the Equals () method, when it compares two objects by equals (), it actually compares two objects to be the same object. This is equivalent to comparing the two objects by "= =".
(02) We can override the Equals () method of the class to let equals () compare the equality of two objects in other ways. The common practice is that the Equals () method returns True if the contents of two objects are equal, otherwise, returns FASLE.
Below, for example, the above 2 kinds of cases are described.
1. "No coverage of the Equals () method"
The code is as follows (Equalstest1.java):
Import java.util.*;
Import java.lang.Comparable;
/**
* @desc the test procedure for Equals (). * *
@author skywang
* @emai kuiwu-wang@163.com/public
class equalstest1{public
static void Main (string[] args) {
//new 2 person objects of the same content,
//Then compare them to equal person
p1 = new Man ("Eee", 100); C13/>person P2 = new Person ("Eee");
System.out.printf ("%sn", P1.equals (p2));
}
/**
* @desc Person class.
* *
private static class person {
int age;
String name;
Public person (String name, int age) {
this.name = name;
This.age = age;
}
Public String toString () {return
name + '-' +age}}}
Run Result:
False
Results Analysis:
We use P1.equals (p2) to "compare P1 and P2 equality". In effect, the Object.java equals () method of the call (P1==P2) is invoked. It is the comparison between "P1 and P2 is the same object".
The definitions of P1 and P2 know that they are the same, but they are two different objects! Therefore, the return result is false.
2. "Coverage of Equals () method"
We modify the above Equalstest1.java: Overwrite Equals () method.
The code is as follows (Equalstest2.java):
Import java.util.*;
Import java.lang.Comparable;
/** * @desc the test procedure for Equals (). * * @author Skywang * @emai kuiwu-wang@163.com/public class equalstest2{public static void Main (string[)
args) {//New Person object with 2 identical content,//again to compare them with equals person P1 = new Man ("Eee", 100);
person P2 = new Person ("Eee", 100);
System.out.printf ("%sn", P1.equals (p2));
/** * @desc the person class.
* * private static class Person {int age;
String name;
Public person (String name, int age) {this.name = name;
This.age = age;
Public String toString () {return name + '-' +age; /** * @desc Overwrite equals method */@Override public boolean equals (Object obj)
{if (obj = = null) {return false; //If the same object returns True, the inverseReturns False if (this = = obj) {return true; }//Judge whether the type is the same if (This.getclass ()!= Obj.getclass ()) {RET
Urn false;
person who = (person) obj;
Return Name.equals (person.name) && age==person.age; }
}
}
Run Result:
True
Results Analysis:
We have overridden the Equals () function of person in Equalstest2.java: Returns True if the name and age of the two man objects are equal.
Therefore, the run result returns TRUE.
By the way, Java's requirements for Equals () are mentioned here. Have the following points:
1. Symmetry: if X.equals (y) returns "true", then Y.equals (x) should also return "true".
2. Reflectivity: X.equals (x) must return is "true".
3. Analogy: If X.equals (y) returns "true" and Y.equals (z) returns "true", then Z.equals (x) should also return "true".
4. Consistency: if X.equals (y) returns "true", as long as the X and Y contents remain unchanged, no matter how many times you repeat X.equals (y), the return is "true".
5. Non-null, x.equals (NULL), always returns "false"; X.equals (and x different types of objects) return forever "false".
Now, review the role of Equals (): Determine whether two objects are equal. When we rewrite equals (), we must not be able to change the role of it!
The 2nd part equals () and = = What is the difference?
= =: Its function is to judge the address of two objects is not equal. That is, to judge two objects is not to try the same object.
Equals (): Its function is also to judge whether two objects are equal. However, it generally has two uses (described in detail in the previous 1th part):
Case 1, the class does not overwrite the Equals () method. Comparing the two objects of the class by Equals () is equivalent to comparing the two objects by "= =".
In case 2, the class overrides the Equals () method. In general, we all override the Equals () method to have the contents of two objects equal, and if their contents are equal, return True (that is, consider the two objects equal).
Below, compare the differences between them by example.