Yes, by default the equals method implements ==in the class Object. But you can override the method equalsin your own class to change the way equalitybetween two objects of the same class. For example, a method equalsin a class is Stringredefined as follows:
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;
}
}
return false;
}
, , :
String s1 = new String("java");
String s2 = new String("java");
s1==s2 false, . s1.equals(s2) true, equals, String, String contents String.