object对象的里面的equal方法
public boolean equals(Object obj) {
return (this == obj);
}
Object类中equals返回的是==的判断,所以在Object类中equals和==没有区别
String对象里面的equal方法(字符串池的概念)
1、字符串池的概念(都是从一个池里面出来的肯定相同)
2、其次再比较内容是否相同
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;
}
String a="1234"; 与 String b=new String("1234") ; 。
b在创建之初会判断有没有“1234”对象,有的话就将对象的地址赋值给b 所有,a.equals(b)是相等的,但是c=“uuid”,这个肯定不存在,也就是肯定没有的。
Interge 里面的方法
Interge 类型的就很简单了就是比较的值是不是相同
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
注意 -128到127 都在integer.cache里面所以 new Integer(126)和new Integer(126) 这两个对象使用==是相同的,但是new Integer(128)和new Integer(128)是不同的