Object中的equals方法如下:
public boolean[color=red] equals[/color](Object obj) {
return (this == obj);
}
看出Object中的equals方法等价于==;
String中的equals方法
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中的equals()是判断当前字符串与传进来的字符串是否一样。
public boolean[color=red] equals[/color](Object obj) {
return (this == obj);
}
看出Object中的equals方法等价于==;
String中的equals方法
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中的equals()是判断当前字符串与传进来的字符串是否一样。