• int、Integer、new Integer和Integer.valueOf()的 ==、equals比较


    Java 基本数据类型的包装类型的大部分都用到了缓存机制来提升性能。

    Byte,Short,Integer,Long 这 4 种包装类默认创建了数值 [-128,127] 的相应类型的缓存数据,Character 创建了数值在 [0,127] 范围的缓存数据,Boolean 直接返回 True or False。

    如果超出对应范围仍然会去创建新的对象,缓存的范围区间的大小只是在性能和资源之间的权衡。

    两种浮点数类型的包装类 Float,Double 并没有实现缓存机制。

    Integer i1 = 33;
    Integer i2 = 33;
    System.out.println(i1 == i2);// 输出 true
    
    Float i11 = 333f;
    Float i22 = 333f;
    System.out.println(i11 == i22);// 输出 false
    
    Double i3 = 1.2;
    Double i4 = 1.2;
    System.out.println(i3 == i4);// 输出 false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    一个小示例:

    Integer i1 = 40;
    Integer i2 = new Integer(40);
    System.out.println(i1==i2);
    
    • 1
    • 2
    • 3

    Integer i1=40 这一行代码会发生装箱,也就是说这行代码等价于 Integer i1=Integer.valueOf(40) 。因此,i1 直接使用的是缓存中的对象。而Integer i2 = new Integer(40) 会直接创建新的对象。

    因此,答案是 false 。

    一、Int a = xx;

    两个Int类型变量用“==”比较的是地址,也就是栈中值的大小。

    二、Integer a = xx;Integer a = Integer.valueOf();

    2.1 Integer a = xx

    将Int值赋给Integer变量,系统会自动将这个Int值封装成一个Integer对象。

    比如:Integer a = 100;实际上的操作是:Integer a = Integer.valueOf(100);

    注意:这里Integer.valueOf(),当Int值的范围在-128-127之间时,会通过一个IntegerCache缓存来创建Integer对象;当Int值不在该范围时,直接调用new Integer()来创建对象,因此会出现以下的情况

    public class Main {
        public static void main(String[] args) {
            Integer a = 100;
            Integer aa = 100;
            Integer b = 200;
            Integer bb = 200;
            System.out.println(a == aa);//true
            System.out.println(b == bb);//false
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 基于减少对象创建次数和节省内存的考虑,JVM方法区中缓存了[-128,127]之间的数字。此数字范围内传参则直接返回缓存中的对象,在此之外,直接new出来。

    2.2 Integer. valueOf()

    可以将基本类型int转换为包装类型Integer,或者将String转换成Integer,String如果为Null或“”都会报错

    public class Main {
        public static void main(String[] args) {
            Integer a = 128;
            Integer b = 128;
            Integer c = Integer.valueOf("12");
            System.out.println(a == b);
            System.out.println(c);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输出:
    在这里插入图片描述
    超过127会new一个对象出来就是false不相等了。

    2.3 equals比较的值

    public static void main(String[] args) {
    	Integer i1 = 200;
    	Integer i2 = 200;
    	
    	System.out.println(i1 == i2);
    	System.out.println(i1.equals(i2));
    }	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    equals比较值,输出true:

    false 
    true
    
    • 1
    • 2

    三、new Integer()

    不管值的范围是否在-128-127,new Integer()每次都会创建新的对象,通过==比较两个对象的内存地址

    public class Main {
        public static void main(String[] args) {
            Integer a = new Integer(100);
            Integer b = new Integer(100);
            System.out.println(a == b);//false
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    四、三者之间的比较

    4.1 与int类型比

    不管是new创建的Integer对象,还是通过直接赋值Int值创建的Integer对象,它们与Int类型变量通过“==”进行比较时都会自动拆箱变成Int类型,所以Integer对象和Int变量比较的是内容大小。

    public class Main {
        public static void main(String[] args) {
            int a = 100;
            Integer b = 100;//等价于b=Integer.valueOf(100);
            Integer c = new Integer(100);
            System.out.println(a == b);//true
            System.out.println(a == c);//true
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.2 与new Integer()比

    new创建的Integer对象和直接赋Int值创建的Integer对象使用==比较的是它们的内存地址。

    • 不管是否在-128-127的范围,new 都会新建对象,而Integer b=100则会拿缓存
    public class Main {
        public static void main(String[] args) {
            Integer b = 100;//等价于b=Integer.valueOf(100);
            Integer c = new Integer(100);
            System.out.println(b == c);//false
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Integer.valueOf(49)Integer a = 49;在执行的时候是一样的,会自动调用Integer.valueOf,但是它们和new 地址都是不一样的

    public class Main {
        public static void main(String[] args) {
            Integer a = 49;
            int b = 49;
            Integer c = Integer.valueOf(49);
            Integer d = new Integer(49);
            System.out.println(a == b);//true
            System.out.println(a == c);//true
            System.out.println(b == c);//true
            System.out.println(c == d);//false
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    参考:Java中Int、Integer、Integer.valueOf()、new Integer()之间的区别
    Integer. valueOf()的使用

  • 相关阅读:
    重点区域人员徘徊识别监测系统
    python+java+springboot快递物流之家管理系统
    新版软考高项试题分析精选(四)
    Double保留两位小数
    使springAOP生效不一定要加@EnableAspectJAutoProxy注解
    【学习笔记】[ARC153F] Tri-Colored Paths
    【计算机组成原理】详细分析RAM、RRAM、SRAM以及DRAM 的基本知识
    Java版工程行业管理系统源码-专业的工程管理软件- 工程项目各模块及其功能点清单
    房屋信贷违约风险竞争(kaggle)系列4-基准模型
    Databend 与海外某电信签约:共创海外电信数据仓库新纪元
  • 原文地址:https://blog.csdn.net/m0_46378271/article/details/126708641