Background: Recently in the study of static scanning things, encountered a rule: "Equals (Object obj)" should be overridden along with the "CompareTo (T obj)" method
And then I want to have a deep steak. What's the difference between equals and CompareTo?
First, let's look at how the Equals method under the Java.lang.String class is implemented.
Public Booleanequals (Object anobject) {if( This==anobject) {//Determine the object's address is consistentreturn true; } if(AnObjectinstanceofString) {//Determine if AnObject is a string type string anotherstring=(String) AnObject; Type Conversionsintn =value.length; if(n = =anotherString.value.length) {//Determine if the length of the character array is consistentCharV1[] =value; CharV2[] =Anotherstring.value; inti = 0; while(n--! = 0{//From left to right to compare characters for equalityif(V1[i]! =V2[i])return false; I++; } return true; } } return false; }
1. The address of the object is consistent returns True
2. Object address inconsistency but the type is consistent, the character array is the same length and each character is equal, returns True
3. Returns False if the argument is not of type string
4. Returns False if the argument is of type string but the length of the character array is inconsistent
5. If the argument is of type string, the character array is the same length, but left to right returns false if a character inconsistency is encountered
Equal (Object obj) method for depth parsing of java.lang.String classes