Let's take a look at the Code:
The output result of this Code is:
Ture
True
--------------
False
It seems that the object class is special. Let's take a look at the source code of the equals () method under the object.
The queals () method in an object is very simple. It simply determines whether the two references point to the same object. If yes, true is returned. If no, false is returned.
However, it is obviously incorrect to apply this method to the string object of the code above, so the equals () method must be rewritten in the string class. Let's take a look at its source code:
The source code of this method indicates that the equals () method of the string class works as follows:
1) determine whether two references point to the same object first. If yes, true is returned directly.
2) Determine whether the real parameter is a string instance. If not, false is returned directly.
3) if the real parameter is an instance of string, compare the strings passed in with the current character one by one to see if they are consistent.
Summary:
A) equals () method: This method is defined in the object class. Therefore, every class in Java has this method. For the equals () method of the object class, it determines whether the caller and the passed reference point to the same object. Therefore, for object classes, the equals () method is equivalent to =.
B) For the string class, the equals () method determines whether the content of the current string is consistent with that of the passed string.
Explanation of the string equals () method