> 文档中心 > Int,Integer的一些理解

Int,Integer的一些理解

 

package num;/** * @author Zeyu Wan * @version 1.0.0 * @ClassName NumberTest2.java * @Description Number Test * @createTime 2022年03月21日 13:15:00 */public class NumberTest2 {    public static void main(String[] args) { float f1 = 19920.0f; int f2 = 19920; long f3 = 19920L; Integer f4 = 19920; Integer f5 = 19920; Integer f6 = new Integer(19920); Integer f7 = 123; Integer f8 = 123; Integer f9 = new Integer(19920); float f10 = 19920.0001f; double f11 = 19920.000000000001; //不同精度的两个数比较的时候会把精度低的隐式转换成精度高的一起比较 true System.out.println(f1==f2); //不同精度的两个数比较的时候会把精度低的隐式转换成精度高的一起比较 true System.out.println(f2==f3); //int和Integer(无论是否是new出来的)比较,都为true,因为Integer会自动拆箱为int再去比较 true System.out.println(f2==f4); System.out.println(f2==f6); //两个Integer相互比较,==比较的是地址,两个地址不相等 false System.out.println(f4==f5); //Integer在享元模式范围之内(-128到127之间),指向的是常量池中的对象,所以相等 true System.out.println(f7==f8); //两个都是new出来的Integer,==比较都为false。若要比较值是否相等,需使用equals()方法进行比较; false System.out.println(f6==f9); //Integer复写了.equals方法,变成了比较值 true System.out.println(f6.equals(f9)); //无论如何,Integer和new Integer()不会相等。不会经历拆箱过程,new Integer指向的是堆中的引用, Integer是在常量池中,地址不同,所以false System.out.println(f5==f6); //float精度丢失 true System.out.println(f1==f10); System.out.println(f2==f10); //double精度丢失 true System.out.println(f1==f11); System.out.println(f2==f11);    }}