The Equals function is defined in the base class object and the source code is as follows
Copy Code code as follows:
public boolean equals (Object obj) {
return (this = = obj);
}
From the source can see the default equals () method and "= =" is consistent, are references to the objects of comparison, not to object values (here is the comparison with our common sense equals () for objects, because most classes in Java rewrite the Equals () method, and the 饽. The following is a string class example, the string equals () method source code is as follows:
[Java]
Copy Code code as follows:
/** The value is used for character storage. */
Private final char value[];
/** the ' offset is ' the ' storage ' is used. */
private final int offset;
/** The count is the number of characters in the String. */
private final int count;
[Java] View plaincopyprint?
public boolean equals (Object anobject) {
if (this = = AnObject) {
return true;
}
if (anobject instanceof String) {
String anotherstring = (string) anobject;
int n = count;
if (n = = Anotherstring.count) {
Char v1[] = value;
Char v2[] = Anotherstring.value;
int i = offset;
int j = Anotherstring.offset;
while (n--! = 0) {
if (v1[i++]!= v2[j++])
return false;
}
return true;
}
}//www.software8.co
return false;
}
The Equals () of the string class is very simple, except that the string class is converted to a character array, bitwise compared.
In summary, using the Equals () method we should note:
1. How Equals () applies the custom object, you must override the system's Equals () method in the custom class.
2. Small knowledge, big trouble.