Java – why string “= =” in equals?
Why does Java compare (this = = another string) in the equalsignorecase method to check that the string is insensitive?
In addition, string equals is a comparison (this = = another string) to compare two objects?
Java 6: the string class equalsignorecase implementation given below
public boolean equalsIgnoreCase(String anotherString) { return (this == anotherString) ? true : (anotherString != null) && (anotherString.count == count) && regionMatches(true,anotherString,count); }
Java 6: String class is equal to the implementation given below
public boolean equals(Object anObject) { if (this == anObject) { return true; }
Solution
This is an optimization If the incoming reference is exactly the same, equals must return true, but we don't need to look at any fields, etc Everything is the same as itself From object Documents for equals (object):
Equality checks are usually carried out by:
>Is the other reference equal to this? If so, return true. > Is the other reference null? If yes, false is returned. > Does another reference refer to an object of the wrong type? If yes, false is returned
Then you continue with a specific type of examination