http://bbs.csdn.net/topics/390000725
Summarize:
equals before the rewrite and = =, after rewriting, equals as long as the content is true
equals = = is generally equivalent, but for string types, it overrides the Equals method and compares the contents. By default, two is the reference address of the comparison, unless you override the Equals method.
equals Source:
Public Booleanequals (Object anobject) {if( This==anobject) { return true; } if(AnObjectinstanceofString) {String anotherstring=(String) AnObject; intn =count; if(n = =anotherstring.count) {CharV1[] =value; CharV2[] =Anotherstring.value; inti =offset; intj =Anotherstring.offset; while(n--! = 0) { if(v1[i++]! = v2[j++]) return false; } return true; } } return false; }
Moderator Answer:
"but often say = = on both sides of the object is compared by address, and equals () is based on the content in the comparison" this sentence in the exclusion of the root class object and did not rewrite the Equals method of the case is correct.
That is, the implementation of equals in the object class is used = =
Other equals methods that inherit object basically override this method, such as the Equals method of the String class:
xml/html Code?
| 123456789101112 |
equalspublic boolean equals(Object anObject)Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Overrides:equals in class ObjectParameters:anObject - The object to compare this String against Returns:true if the given object represents a String equivalent to this string, false otherwiseSee Also:compareTo(String), equalsIgnoreCase(String) |
This is the Equals method of string source code:
Java Code?
| 12345678910111213141516171819202122 |
public booleanequals(Object obj) { if(this== obj) returntrue; if(obj instanceofString) { String s = (String)obj; inti = count; if(i == s.count) { charac[] = value; charac1[] = s.value; int j = offset; intk = s.offset; while(i-- != 0) if(ac[j++] != ac1[k++]) returnfalse; returntrue; } } returnfalse; } |
Java Interview question: Equals () method and = = Difference