简单地说“==”是值比较。 equals()是对象比较 举个例子吧: int a=2; int b=2; a==b返回的是true,但a.equals(b)是不允许的; Integer c = new Integer(2); Integer d = new Integer(2); c.equals(d) 返回值是true 而c==d返回的是false(c和d此时成了对象)一般容易在String上犯错误. 要注意的有两点: 一.String 是一 个对象Object! 二.java 中有String pool的概念,在String pool 中只有一个相同对象的实例. 如String s1="aaa"; String s2="aaa"; 在String pool中,只有一个aaa 所以s1==s2是true s1.equals(s2)也是true 明白了吧!
class如果不实现equals()方法,和==效果是一样的,很多class你可以实现自己的equals方法。比如String类,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; }
When you use == with a primitive -int, double, char, ... you are checking that the values are identical. But if you use == with an object, you are checking that the 2 objects are stored at the same address. In other words the references pointing to the same object... Method equals () is different. It is the same as ==, if it isn't overriden by the object class. Many classes override the method equals (). In this case this method will check that content of the object is the same or not, not addresses.
The == operators are straightforward when the operands are primitives. When the operands are references, the operators test for referece quality. This means that the refereces are checked to see if they point to the same object.(This is distinct from object equality, under which two different objects are considered equal if their significant instance variables are equal.)
equals不过是一个成员方法,如果愿意可以继承String 且改写它
比如比较头一个字符就算相等…
equals()是对象比较
举个例子吧:
int a=2;
int b=2;
a==b返回的是true,但a.equals(b)是不允许的;
Integer c = new Integer(2);
Integer d = new Integer(2);
c.equals(d) 返回值是true
而c==d返回的是false(c和d此时成了对象)一般容易在String上犯错误.
要注意的有两点:
一.String 是一 个对象Object!
二.java 中有String pool的概念,在String pool 中只有一个相同对象的实例.
如String s1="aaa";
String s2="aaa";
在String pool中,只有一个aaa
所以s1==s2是true
s1.equals(s2)也是true
明白了吧!
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;
}
the values are identical. But if you use == with an object, you are checking that the 2
objects are stored at the same address. In other words the references pointing to the
same object...
Method equals () is different.
It is the same as ==, if it isn't overriden by the object class.
Many classes override the method equals (). In this case this method will check that
content of the object is the same or not, not addresses.
equals()在Object中的定义和==是一致的,但是作为函数他是可以被重载的,比如
在String和Boolean中,它就被重载为比较两个对象的值是否相等
“equals()”是Object类的一个成员函数。
String s1="123";
String s2="123";
s1==s2 //返回为false
s1.equals(s2) //返回为真
String s1="123";
String s2="123";
s1==s2 //返回为false----------------------是false吗???
s1.equals(s2) //返回为真
类的对象用equals 如String,Integer