面试常客
Object o = new Object();
Object o1 = new Object();
if (o == o1){
System.out.println("==");
}else {
System.out.println("111" + o);
System.out.println("111" + o1);
}
if (o.equals(o1)){
System.out.println("equals");
}else {
System.out.println("222" + o);
System.out.println("222" + o1);
}
==与equals都是比较的是内存地址,区别就是equals是object的方法
String的equals是被重写的了,所以比较的是值
String str1 = "123";
String str2 = "123";
if (str1 == str2){
System.out.println("一样");
}else {
System.out.println("不一样");
}
if (str1.equals(str2)){
System.out.println("一样");
}else {
System.out.println("不一样");
}
看看String的equals底层源码
PS:上文中的图片或资源来源于网络或本人原创,仅供学习与参考,如有侵权,请私信作者进行删除。