帮我解释一下String类中equals()源码
private final char value[];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;
}
把这些复制到java类中回报错,内部定义为什么可以呢?我们能否复写String的equals()方法?String不是被final修饰了吗?