public class BTest {
public static void main(String[] args) {
int idcard = 110000000;//基本数据类型
Long idCardO = new Long(110000000);//包装类型
System.out.println(idcard==idCardO);//基本数据类型和包装类型比较
Long idCardO2 =new Long(110000000);
System.out.println(idCardO2==idCardO);//基本数据类型和包装类型比较
}
}
输出:
true
false
备注:idcard,idCardO,idCardO2三个变量的值都是数字110000000。
1. idCardO2==idCardO 等价于 对象 == 对象
2. idcard == idCardO 等价于 基本数据类型 == 对象
1.对象==对象
对象的==比较,比较的是地址。idCardO,idCardO2是new的两个对象,所以地址不同,输出false
2.基本数据类型long == Long对象
查看class字节码,class文件里代码为idcard == idCard.longValue()。编译器进行了代码校正。
即idcard==idCardO实际为idcard == idCard.longValue()。输出true
如若转载,请注明出处:https://www.javaidea.cn/article/7521.html