Java包装类比较和包装类底层缓存数组

关于包装类的比较问题,我们先看以下代码:

Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 200;
Integer i4 = 200;
System.out.println(i1 == i2); // true
System.out.println(i3 == i4); // false

Double d1 = 100.0;
Double d2 = 100.0;
Double d3 = 200.0;
Double d4 = 200.0;
System.out.println(d1 == d2); //false
System.out.println(d3 == d4); //false

为什么 i1和 i2 比较为true,i3和i4比较为false呢?

这是因为直接将一个 基本类型的数字赋值给包装类发生了自动装箱,会隐式调用Integer的静态方法valueOf()方法,下面我们看一下valueOf()方法的源码

public static Integer valueOf(int i) {
     if (i >= IntegerCache.low && i <= IntegerCache.high)
          return IntegerCache.cache[i + (-IntegerCache.low)];
     return new Integer(i);
}

通过源码发现,在new Integer()之前会先判断传入的值是否在IntegerCache的cache数组范围里,如果在的话就直接返回引用。不在的话,创建一个新的Integer对象。

下面我们看一下IntegerCache源码:

    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                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() {}
    }

通过源码我们发现IntegerCache会默认把-128到127保存到cache数组里面。因此,你如果多次传入 100 ,它返回的对象都是同一个对象。

这时我们回到上面的题目,就可以知道原因了,因为使用 == 比较的是地址,当传入两个100,在IntegerCache的cache数组范围内,返回同一对象,所以比较结果为true,当传入的为200 时,不在数组范围内,所以每次都会创建一个新的对象,所以比较结果为false。

那为什么Double包装类比较结果都是false呢?

虽然Double 于与 Integer 类相似都会调用valueOf()方法,但是在指定范围内浮点型数据个数不确定,而整型的个数是确定的,所以Double 是不可以缓存的,它每次都会返回一个新的对象。

注意:

  1. Integer,Short,Byte,Character,Long 的valueOf方法实现类似,而Double和Float 比较特殊,每次返回新包装对象。
  2. 如果是使用 new 创建的,不会缓存,每次都会返回一个新对象

下面我们继续看包装类的比较

Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
Long h = 2L;

System.out.println(c == d); //true
System.out.println(e == f); //false

System.out.println(c == (a + b)); //true
System.out.println(c.equals(a + b));//true

System.out.println(g == (a + b)); //true
System.out.println(g.equals(a + b));//false
System.out.println(g.equals(a + h));//true

Integer a2 = 444;
int c3 = 444;
System.out.println(a2 == c3); // true
System.out.println(a2.equals(c3));//true

 通过上述代码我们可以得出以下结论:

  • 对于两边都是包装类型的比较 == 比较的是地址,equals比较的是值。
  • 对于两边有一边是表达式(包含算数运算),则 == 比较的是数值(自动触发拆箱过程),对于包装类型 equals方法不会进行类型转换。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值