Java包装类对象的比较

说明

"一切皆对象" 这句话在 Java 里并不是很正确,在 Java 里除了对象,还有 int,short...这些基本数据类型,但是基本数据类型在操作的时候有时候并不是很方便,因此有了包装类对象这个概念,因为有自动装箱,拆箱技术,所以我们在使用过程中感觉和基本数据类型没什么差别,但在比较相等的时候,还是有几个小坑.

首先先判断下,下面几个表达式的的值

int a = 1;
Integer b = 1;
Integer c = new Integer(1);

Integer d = 1;
Integer e = new Integer(1);

Integer f = 1000;
Integer g = 1000;

System.out.println("1.a==b? " + (a==b))
System.out.println("2.b==c? " + (b==c))
System.out.println("3.a==c? " + (a==c))
System.out.println("4.b==d? " + (b==d))
System.out.println("5.c==e? " + (c==e))
System.out.println("6.f==g? " + (f==g))
复制代码

结果分析

挨个分析

a==b & a==c

这两个都为true

有一边是基本类型,那比较的就是值

b==c & c==e

false

比较两个对象的地址,所以为false

b==d & f==g

前者为true,后者为false

是不是很奇怪,理应这两个的结果应该是一样的

首先 Integer d = 1 ,这是一个自动装箱的功能,其实调用的是Integer d = Integer.valueOf(1),看下源码

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

所以答案就很明确了,再去查看IntegerCache.low 和 IntegerCache.high,其中 low 的值为-128,high的值就比较复杂了点,默认要是没有设置java.lang.Integer.IntegerCache.high的话,那么这个值就是127,所以这也很明显了

 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 =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                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);
            }
            high = h;

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

复制代码

再看下面的例子,全部为true,这是因为只要一边有数值运算,那么比较的就是值

System.out.println("1.a==b? " + (a==b+0))
System.out.println("2.b==c? " + (b==c+0))
System.out.println("3.a==c? " + (a==c+0))
System.out.println("4.b==d? " + (b==d+0))
System.out.println("5.c==e? " + (c==e+0))
System.out.println("6.f==g? " + (f==g+0))
复制代码

小节

当包装类对象和基本数据类型比较的时候

  1. 如果有一边是基本类型,那比较的就是值
  2. 如果两边是对象,比较的是地址,如果是自动装箱生成的,那么在IntegerCache.low 到 IntegerCache.high的范围内是相等,因为是同个对象,超过这范围就会重新生成对象
  3. 如果一边有计算表达式,那么就是比较的值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值