引用类型的比较:

==:比较的是实例的引用的值(既实例的地址) 若相等则true 否则false

Object.equals(obj):

Object中的equals方法 this==obj实现, a.equal(b) 对于非空的a,b,当且仅当指向同一实例时 返回true,所以一般要重写

String.equals(s):因为重写了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;
        }

posted on 2012-08-03 17:04  Death_Fat  阅读(150)  评论(0)    收藏  举报