Brief introduction of equals method of object class & String class rewrites equals method

The source code of equals method in the object class is as follows

1 public boolean equals(Object obj)  
2 {  
3     return this == obj;  
4 }  

The equals method in Object directly determines whether the values of this and obj are equal. That is to say, it is used to determine whether the Object calling equals and the Object referenced by parameter obj are the same Object. The so-called same Object refers to the same memory unit in memory. If this and obj point to the same memory pair of hi

If this and obj do not point to the same block of memory, then return true. Note: even two different memory objects with identical contents will return false.

If it is the same block of memory, the equals method in object returns true. If it is different memory, it returns false.

If you want to return true when two objects with different memory but the same content are equal, we need to override the equal method of the parent class

[reproduced from]

The String class overrides the equals method in the object

 1  /**
 2      * Compares this string to the specified object.  The result is {@code
 3      * true} if and only if the argument is not {@code null} and is a {@code
 4      * String} object that represents the same sequence of characters as this
 5      * object.
 6      *
 7      * @param  anObject
 8      *         The object to compare this {@code String} against
 9      *
10      * @return  {@code true} if the given object represents a {@code String}
11      *          equivalent to this string, {@code false} otherwise
12      *
13      * @see  #compareTo(String)
14      * @see  #equalsIgnoreCase(String)
15      */
16     public boolean equals(Object anObject) {
17         if (this == anObject) {
18             return true;
19         }
20         if (anObject instanceof String) {
21             String anotherString = (String)anObject;
22             int n = value.length;
23             if (n == anotherString.value.length) {
24                 char v1[] = value;
25                 char v2[] = anotherString.value;
26                 int i = 0;
27                 while (n-- != 0) {
28                     if (v1[i] != v2[i])
29                         return false;
30                     i++;
31                 }
32                 return true;
33             }
34         }
35         return false;
36     }

Judgment conditions:

Returns true if two objects have the same reference, which is the method in the Object class

Returns false if the object passed in does not belong to the String class

If the passed in object belongs to the String class, compare with the length of the original object. If the length is not equal, return false. If it is equal, compare their contents one by one. If they are the same, return true. Otherwise, return false

Note: this method does not judge whether two objects are null, that is to say:

1  public static void main(String args[]){
2         String a = null;
3         String b = null;
4         System.out.println(a.equals(b));
5     }

Running this code will result in a null pointer exception, and the object (a in this case) calling the equals method is not allowed to be null

Keywords: Java

Added by ben.hornshaw on Fri, 22 May 2020 18:33:03 +0300