Master the usage of equals through the following examples
package cn.galc.test;public class TestEquals { public static void main(String[] args) { /** * Here, the construction method Cat() is used to create two new cats in the heap memory. * The two cats are Color, weight, and height are all the same, * but c1 and c2 will never be equal. This is because c1 and c2 are reference objects of the two cats in the heap memory, * It contains the addresses where the two cats can be found, but since the two cats are stored in two different spaces in the heap memory, * c1 and c2 contain different addresses, so c1 and c2 will never be equal. */ Cat c1 = new Cat(1, 1, 1); Cat c2 = new Cat(1, 1, 1); System.out.println("The result of c1==c2 is: "+(c1==c2 ));//false System.out.println("The result of c1.equals(c2) is: "+c1.equals(c2));//false }}class Cat { int color, weight, height; public Cat(int color, int weight, int height) { this.color = color; this.weight = weight; this.height = height; }}Draw a memory analysis diagram to analyze the results of comparison between c1 and c2
program:
Cat c1 = new Cat(1,1,1);Cat c2 = new Cat(1,1,1);
After execution, the layout in the memory is as shown below:
c1 points to an object, and c2 also points to an object. C1 and c2 contain the addresses stored in the heap memory of these two Cat objects. Since the two Cat objects are located in different storage spaces, c1 and c2 contain The addresses of are definitely not equal, so the two reference objects c1 and c2 are definitely not equal. Therefore, if you execute: "System.out.println(c1==c2);" the printed result will definitely be false. So you create two objects with new. Don't worry, the references of the two objects will never be the same. If they are the same, one of them will be overwritten. This is not possible. Whether c1 is equal to c2 compares the contents contained in the two references c1 and c2, because the references of the two objects produced by new are never the same, so the contents of the two references c1 and c2 are also never the same. Therefore c1 can never equal c2. Therefore, comparing the references of two objects can never make the two objects equal or identical.
To determine whether two objects are equal, you cannot compare whether the references of the two objects are equal. You will never get an equal result because the references of the two objects will never be equal, so the correct comparison method is to directly Compare these two objects and compare whether the essence of the two objects is the same, that is, whether the contents of the two objects are the same. Determine whether the two objects are equal by comparing whether the attribute values of the two objects are the same. .
The Object class provides an equals() method to compare whether the contents of two objects are the same, so we can use this method to compare whether the two objects are logically "equal". For example: c1.equals(c2); Here is the call to the equals() method inherited from the Object class. By consulting the API documentation, the definition of the equals method in the Object class is as follows:
public boolean equals(Object obj)
The default implementation of the Equals() method provided in the Object class is to compare the reference of the current object and the reference you want to compare to see if they point to the same object, which is the same as "c1==c2". , "c1.equals(c2)" and "c1==c2" are completely equivalent. Therefore, directly using the inherited equals() method cannot directly compare whether the contents of two objects are the same. For this reason, we must override the equals() method and change the default implementation of this method.
Next, rewrite the inherited equals() method in the Cat class:
class Cat { int color, weight, height; public Cat(int color, int weight, int height) { this.color = color; this.weight = weight; this.height = height; } /** * This is rewriting Equality The equals() method inherited from the Object class changes the default implementation of this method. * Use our own defined implementation to determine whether two objects are logically equal. * Here we define that if the color, weight, and height of two cats are the same, * then we think that the two cats are logically identical, that is, the two cats are "equal". */ public boolean equals(Object obj){ if (obj==null){ return false; } else{ /** * instanceof is an object operator. * The object operator is used to determine whether an object belongs to an instance of a specified class or specified subclass. * The object operator is a combined word instanceof. * This operator is a binary operator. The expression on the left is an object and the expression on the right is a class. * If the object on the left is an object created by the class on the right, the result of the operation is true, otherwise it is false. . */ if (obj instanceof Cat){ Cat c = (Cat)obj; if (c.color==this.color && c.weight==this.weight && c.height==this.height){ return true; } } } return false; }} At this time, execute the printing command in the main method:
public static void main(String[] args) { /** * Here, the construction method Cat() is used to create two new cats in the heap memory. * The color, weight, and height of these two cats are the same. * But c1 and c2 will never be equal. This is because c1 and c2 are reference objects of the two cats in the heap memory. * contains the addresses where the two cats can be found, but because the two cats are stored in the heap memory In two different spaces, * So c1 and c2 hold different addresses, so c1 and c2 will never be equal. */ Cat c1 = new Cat(1, 1, 1); Cat c2 = new Cat(1, 1, 1); System.out.println("The result of c1==c2 is: "+(c1==c2 ));//false System.out.println("The result of c1.equals(c2) is: "+c1.equals(c2));//true }The result obtained this time is different from the result obtained last time without overriding the equals() method:
"System.out.println(c1 == c2);" The printed result is still false, because the contents of the references of the two objects are compared. The contents of the two references are of course not equal, and will never be are equal, so the printed result must be false.
"System.out.println(c1.equals(c2));" The printed result is true, because we have rewritten the equals() method in the Cat class and changed the default implementation of this method. We have changed the implementation of the method Change to only If these two objects really exist and are both cats, and their color, height and weight are the same, then the two cats are logically identical and exactly the same. Two cats, that is, these two cats are "equal". So the result printed here is true.
So how to compare two string objects for equality?
Look at the following example:
public class TestEquals { public static void main(String args[]){ String s1 = new String("hello"); String s2 = new String("hello"); System.out.println("s1 == s2 result Is: "+(s1 == s2));//false System.out.println("The result of s1.equals(s2) is: "+s1.equals(s2));//true }}This time the two string objects are compared for equality:
System.out.println(s1 == s2);
The printed result is still false, because the references of the two string objects s1 and s2 are compared here. The references of the two objects will never be equal, so the printed result is false.
System.out.println(s1.equals(s2));
The printed result is true, because the inheritance from the Object class is rewritten in the String class (all classes are inherited from the Object class, and the String class is of course no exception. If you inherit from the parent class, you have everything of the parent class. Attributes and methods, so the Sting class also has the equals() method, and the inherited equals() method has also been rewritten), changing the default implementation of this method.
In the String class, the implementation of the equals() method is overridden like this: compare the current string object with the specified string object. The specified string object cannot be empty and the character sequence of this object is the same as the current string. The string sequences of the objects are the same. If these conditions are met, then the two string objects are equal.
Therefore, s2 here has met the condition, so the printed result is true.
In the future, when comparing two objects in a certain class for equality, first go to the API documentation to find out whether this class has overridden the equals() method inherited from the Object class. If the equals() method is overridden, then the overridden equals() method is called when comparing whether two objects are equal. If it is not overridden, then the one inherited from the Object class is called directly. equals() method, and uses the default implementation of equals() method to compare whether two objects are equal. Therefore, each class can override the equals() method inherited from the Object class as needed.
For finding a certain class in the API document, if a class can be used directly without introducing a package, then this class must be in the java.lang package. For example, the String class here can be used directly, so the String class must be It is in the java.lang package. When using a certain class, check which package the class imports, and then go to the package to find the class. Classes that do not need to import packages must be located in java.lang. Just go directly to java.lang to find it.
Generally, when we design a class, we need to override the equals method of the parent class. When rewriting this method, we need to design it according to the following rules:
1. Reflexivity: For any reference value X, the return value of x.equals(x) must be true.
2. Symmetry: For any reference value x, y, if and only if the return value of y.equals(x) is true, the return value of x.equals(y) must be true;
3. Transitivity: If x.equals(y)=true, y.equals(z)=true, then x.equals(z)=true
4. Consistency: If the objects participating in the comparison do not change, the result of the object comparison should not change either.
5. Non-nullability: For any non-null reference value X, the return value of x.equals(null) must be false
For example:
public class People { private String firstName; private String lastName; private int age; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; People other = (People) obj; if (age != other.age) return false; if (firstName == null) { if (other.firstName != null) return false; } else if (!firstName.equals(other.firstName)) return false; if (lastName == null) { if (other.lastName != null) return false; } else if (!lastName.equals(other.lastName)) return false ; return true; }}
In this example, we stipulate that a person is the same person if his last name, first name and age are the same. Of course, you can also add other attributes. For example, the ID number must be the same to be judged as the same person. Then you can add the ID number judgment in the equals method!
Summary: To compare whether two objects are equal, we use the equals() method. The conditions for judging whether two objects are equal are defined by us after rewriting the implementation of the equals() method, so that the equals() method can be used more flexibly. Compare two objects of the same class in different classes to see if they are equal.