int Integer之间使用 == 比较结果分析

前置知识:

java中使用 == 进行比较,基本数据类型(byte、short、int、long、float、double、char、boolean)比较的是值,引用类型比较的是地址。
使用equals()进行比较时,没有重写equals()方法,底层用的也是用 == 进行比较。

int Integer使用 == 比较分析

  1. int 和 Integer的==比较时,integer自动拆箱成int,相当于基本类型的比较

  2. Integer c = 10 和 Integer c1 = 10的==比较
    首先int型的10自动装箱为Integer型,相当于调用了Integer.valueOf(10)的方法
    自动装箱时首先判断i值是否在[-128,127]这个区间中,如果在这个区间中,则直接从IntegerCache.cache缓存中获取指定数字的包装类,
    不在这个区间中则new出一个新的包装类
    所以在[-128,127]之间返回true,超出则相当于创建出一个新的Integer对象,地址不同,则结果为false

    IntegerCache内部实现了一个Integer的静态常量数组,在类加载的时候,执行static静态块进行初始化[-128,127]之间的Integer对象,
    存放到cache数组中,cache数组属于常量,存放在java的方法区中,这样调用时会使用缓存池中的对象,多次调用会取得同一个对象的引用

  3. Integer c = 10 和 Integer c1 = new Integer(10)的==比较
    不同的对象,地址不同,结果当然不同

  4. Integer c = new Integer(10) 和 Integer c1 = new Integer(10)的==比较
    不同的对象,地址不同,结果当然不同

代码示例

public static void test(){
        int a = 10;
        int b = 1000;
        Integer c = 10;
        Integer c1 = 10;
        Integer d = 1000;
        Integer d1 = 1000;
        Integer e = new Integer(10);
        Integer e1 = new Integer(10);
        Integer f = new Integer(1000);
        Integer f1 = new Integer(1000);

        /**
         * int 和 Integer的==比较时,integer自动拆箱成int,相当于基本类型的比较
         */
        System.out.println(a == c); // true
        System.out.println(b == d); // true
        System.out.println(a == e); // true
        System.out.println(b == f); // true

        /**
         * Integer c = 10 和 Integer c1 = 10的==比较
         * 首先int型的10自动装箱为Integer型,相当于调用了Integer.valueOf(10)的方法
         * 自动装箱时首先判断i值是否在[-128,127]这个区间中,如果在这个区间中,则直接从IntegerCache.cache缓存中获取指定数字的包装类,
         * 不在这个区间中则new出一个新的包装类
         * 所以在[-128,127]之间返回true,超出则相当于创建出一个新的Integer对象,地址不同,则结果为false
         *
         * IntegerCache内部实现了一个Integer的静态常量数组,在类加载的时候,执行static静态块进行初始化[-128,127]之间的Integer对象,
         * 存放到cache数组中,cache数组属于常量,存放在java的方法区中,这样调用时会使用缓存池中的对象,多次调用会取得同一个对象的引用
         */
        System.out.println(c == c1); // true
        System.out.println(d == d1); // false

        /**
         * Integer c = 10 和 Integer c1 = new Integer(10)的==比较
         * 不同的对象,地址不同,结果当然不同
         */
        System.out.println(c == e); // false
        System.out.println(c1 == e1); // false

        /**
         * Integer c = new Integer(10) 和 Integer c1 = new Integer(10)的==比较
         * 不同的对象,地址不同,结果当然不同
         */
        System.out.println(e == e1); // false
        System.out.println(f == f1); // false
    }

Integer内部类IntegerCache源码

/**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */

    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[]; // [-128-127]常量缓存池

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenzm666666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值