String 源码解析
// String是引用类型,比较时不能比较引用是否相等,而是比较字符串的内容是否相等
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
//一般重写equals 方法先写引用是否相等,再判断对象是不是instance of 某个object,再判断某个具体的key是不是相等