首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

两个String对象a,b if (a==b) then a.equals(b) == ture应该是这么吧

2011-12-05 
两个String对象a,b if (ab) then a.equals(b) ture应该是这样吧?有反例吗?如果两个对象引用相等,那它

两个String对象a,b if (a==b) then a.equals(b) == ture应该是这样吧?
有反例吗?如果两个对象引用相等,那它们引用到的对象肯定是同一个吧。

[解决办法]
当然相同
看java源代码
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;
}

热点排行
Bad Request.