Java包装类对象的比较

444 阅读2分钟

说明

"一切皆对象" 这句话在 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. 如果一边有计算表达式,那么就是比较的值