java中equals与==的对比
class B {
private int num;
B() {
num = 1;
}
}
B a = new B();
B b = new B();
System.out.println(a.equals(b));//false
String str_a = new String("abc");
String str_b = new String("abc");
System.out.println(str_a.equals(str_b)); //true
System.out.println(str_a==str_b); // false
StringBuffer sb1 = new StringBuffer("a");
StringBuffer sb2 = new StringBuffer("a");
System.out.println(sb1.equals(sb2)); //false
System.out.println(sb1==sb2); // false
class A {
private int a;
A() {
a = 1;
}
public boolean equals(A b) {
if (a == b.a)
return true;
else
return false;
}
}
A a = new A();
A b = new A();
System.out.println(a.equals(b)); //true
System.out.println(a == b); //false
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。