1、对于基本数据类型,“==”比较的是值。基本数据类型没有“equals”方法。
基本的数据类型有boolean,char,byte,short,int,long,float,double,void。注意String不是基本数据类型。
int i1 = 10;
int i2 = 10;
System.out.println("i1's identityHashCode=" + System.identityHashCode(i1));
System.out.println("i2's identityHashCode=" + System.identityHashCode(i2));
System.out.println("[i1 == i2] -> " + (i1 == i2) + "\n");
i1 = 10;
i2 = Integer.parseInt("10");
System.out.println("i1's identityHashCode=" + System.identityHashCode(i1));
System.out.println("i2's identityHashCode=" + System.identityHashCode(i2));
System.out.println("[i1 == i2] -> " + (i1 == i2) + "\n");
运行结果:
i1's identityHashCode=4072869
i2's identityHashCode=4072869
[i1 == i2] -> true
i1's identityHashCode=4072869
i2's identityHashCode=4072869
[i1 == i2] -> true
2、对于“非基本数据类型”,“==”比较的是对象的引用值,“equals”比较的也是引用值。可以通过重写equals方法实现值的比较。
class Number {
public Number(int i){
this.i = i;
}
int i;
}
public class Assignment {
public static void main(String[] args) {
Number n1 = new Number(10);
Number n2 = new Number(10);
System.out.println("n1's hashcode=" + n1.hashCode());
System.out.println("n2's hashcode=" + n2.hashCode());
System.out.println("n1's identityHashCode=" + System.identityHashCode(n1));
System.out.println("n2's identityHashCode=" + System.identityHashCode(n2));
System.out.println("[n1 == n2] -> " + (n1 == n2));
System.out.println("[n1.equals(n2)] -> " + (n1.equals(n2)) + "\n");
}
}
运行结果:
n1's hashcode=1671711n2's hashcode=11394033
n1's identityHashCode=1671711
n2's identityHashCode=11394033
[n1 == n2] -> false
[n1.equals(n2)] -> false
此时如果要比较对象的具体内容,而不是比较对象的引用,那么就要重写equals方法。
class Number {
public Number(int i){
this.i = i;
}
int i;
public boolean equals(Object obj){
if(obj != null && obj instanceof Number){
if(this.i == ((Number)obj).i){
return true;
}
}
return false;
}
}
public class Assignment {
public static void main(String[] args) {
Number n1 = new Number(10);
Number n2 = new Number(10);
System.out.println("n1's hashcode=" + n1.hashCode());
System.out.println("n2's hashcode=" + n2.hashCode());
System.out.println("n1's identityHashCode=" + System.identityHashCode(n1));
System.out.println("n2's identityHashCode=" + System.identityHashCode(n2));
System.out.println("[n1 == n2] -> " + (n1 == n2));
System.out.println("[n1.equals(n2)] -> " + (n1.equals(n2)) + "\n");
}
}
运行结果:
n1's hashcode=1671711
n2's hashcode=11394033
n1's identityHashCode=1671711
n2's identityHashCode=11394033
[n1 == n2] -> false
[n1.equals(n2)] -> true
3、String不属于基本数据类型。但是“==”和“equals”的比较与上面的对象比较不一样。
“==”比较的是对象的”引用“,而equals比较的是“值“。
String s1 = "abc";
String s2 = "abc";
System.out.println("s1's hashcode=" + s1.hashCode());
System.out.println("s2's hashcode=" + s2.hashCode());
System.out.println("s1's identityHashCode=" + System.identityHashCode(s1));
System.out.println("s2's identityHashCode=" + System.identityHashCode(s2));
System.out.println("[s1 == s2] -> " + (s1 == s2));
System.out.println("[s1.equals(s2)] -> " + s1.equals(s2) + "\n");
运行结果: 此时,内存中只保留一份”abc“的空间。这个与常量的存储方式有关。因此s1与s2的hashcode一样,值也一样。
s1's hashcode=96354
s2's hashcode=96354
s1's identityHashCode=4072869
s2's identityHashCode=4072869
[s1 == s2] -> true
[s1.equals(s2)] -> true
s1 = new String("abc");
s2 = new String("abc");
System.out.println("s1's hashcode=" + s1.hashCode());
System.out.println("s2's hashcode=" + s2.hashCode());
System.out.println("s1's identityHashCode=" + System.identityHashCode(s1));
System.out.println("s2's identityHashCode=" + System.identityHashCode(s2));
System.out.println("[s1 == s2] -> " + (s1 == s2));
System.out.println("[s1.equals(s2)] -> " + s1.equals(s2) + "\n");
运行结果:此时,s1和s2分别指向两个不同的存储单元。因此hashcode不一样,但是值是一样的。
s1's hashcode=96354
s2's hashcode=96354
s1's identityHashCode=1671711
s2's identityHashCode=11394033
[s1 == s2] -> false
[s1.equals(s2)] -> true
String s1 = "abc";
String s2 = "abcd".substring(0,3);
System.out.println("s1's hashcode=" + s1.hashCode());
System.out.println("s2's hashcode=" + s2.hashCode());
System.out.println("s1's identityHashCode=" + System.identityHashCode(s1));
System.out.println("s2's identityHashCode=" + System.identityHashCode(s2));
System.out.println("[s1 == s2] -> " + (s1 == s2));
System.out.println("[s1.equals(s2)] -> " + s1.equals(s2));
运行结果:这种情况下,也是分别指向两个不同的存储单元。
s1's hashcode=96354
s2's hashcode=96354
s1's identityHashCode=4072869
s2's identityHashCode=1671711
[s1 == s2] -> false
[s1.equals(s2)] -> true
在这里我们也可以看到,”引用“应该是指System.identityHashCode(obj),而不是obj.hashCode();
所以我个人认为String的比较用equals比较。 如果比较是否是同一个引用,那么就用”==“。