1、比较基本类型看数值,忽略类型
public class Test { public static void main(String[] args) throws ClassNotFoundException { int a = 2; double b=2; System.out.println(a==b); } } 结果是:true
2、引用类型比较看是否重写了equal方法,如果重写了,数值相等就相等,如果没有重写调用的是object的equals方法,比较的是地址
public class Test { public static void main(String[] args) throws ClassNotFoundException { User user=new User(); user.setAge(11); user.setEmail("123@qq.com"); User user1=new User(); user1.setAge(11); user1.setEmail("123@qq.com"); System.out.println(user.equals(user1)); } } 没有重写equal方法的,结果是false
public class Test { public static void main(String[] args) throws ClassNotFoundException { User user=new User(); user.setAge(11); user.setEmail("123@qq.com"); User user1=new User(); user1.setAge(11); user1.setEmail("123@qq.com"); System.out.println(user.equals(user1)); } } 重写了equal方法,结果就是true
3、==号比较的是内存地址
public class Test{ public static void main(String[] args) throws ClassNotFoundException { User user=new User(); user.setAge(11); user.setEmail("123@qq.com"); User user1=new User(); user1.setAge(11); user1.setEmail("123@qq.com"); System.out.println(user==user1); } } 结果是:false
原创文章,作者:254126420,如若转载,请注明出处:https://blog.ytso.com/273129.html