Integer的三三两两

Integer的三三两两

题目

//你能明白为什么吗?
private static void test3() {
        int a = 200;
        Integer a1 = 200;
        Integer b1 = 200;
        Integer c1 = 400;
        Integer d1 = a1 + b1;
        System.out.println("---------t3----------");
        System.out.println(a == a1);//true
        System.out.println(a == b1);//true
        System.out.println(a1 == b1);//false
        System.out.println(c1 == (a1 + b1));//true
        System.out.println(c1 == d1);//false
}

一句话总结

默认情况下Integer把一些数字对应的对象提前创建并保存了,当待创建Integer对象处于-128到127之间时,系统不会新创建一个对象,而是把缓存中的那个对象返回.

从IntegerCache讲起

默认情况下Integer在初始化时候,会对-128到127提前创建好对象备用
//Integer.class   ----   IntegerCache方法
private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer[] cache;
    static Integer[] archivedCache;
    static {
        int h = 127;
        //用户启动虚拟机时,可自行设置要创建缓存对象的最大正Integer值
        String integerCacheHighPropValue = VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        int size;
        //若用户设置了这个值
        if (integerCacheHighPropValue != null) {
            try {
                //若用户预设值小于127,则仍使用默认127
                size = Integer.parseInt(integerCacheHighPropValue);
                size = Math.max(size, 127);
                //high值不能超过2147483518,即(Integer.MAX_VALUE - (-low) - 1)
                h = Math.min(size, 2147483518);
            } catch (NumberFormatException var6) {
            }
        }
        high = h;
        VM.initializeFromArchive(Integer.IntegerCache.class);
        //求出size大小
        size = high - -128 + 1;
        if (archivedCache == null || size > archivedCache.length) {
            //创建缓存
            Integer[] c = new Integer[size];
            int j = -128;
            for (int k = 0; k < c.length; ++k) {
                c[k] = new Integer(j++);
            }
            archivedCache = c;
        }
        cache = archivedCache;
        assert high >= 127;
    }
}

Integer执行一次完整过程示例

Integer自动拆装箱

//自动装箱方法
Integer x=100;
public static Integer valueOf(int i) {
    	//若int值处于-128和high值之间,则返回初始化时创建好的缓存.否则创建新对象存储.
        return i >= -128 && i <= Integer.IntegerCache.high ? Integer.IntegerCache.cache[i + 128] : new Integer(i);
    }
//自动拆箱方法
private final int value;
//返回其对应的int值
public int intValue() { return this.value; }

Integer运算

Integer x = 100;
x += 100;
//x+=100完整执行过程如下,Integer每次运算都需要拆箱和装箱各一次
x = Integer.valueOf(x.intValue() + 100);

一些例题

原生int和Integer比较

int i = 34; Integer j = new Integer(34);
System.out.println(i == j);
比较 int i = 34 和 Integer j = new Integer(34);
jvm 发现j和原生类型比较,则进行拆箱操作,即执行intValue方法,返回Integer对象的value成员变量.
System.out.println(i == j) 
    实质为:
System.out.println(i == j.intValue());两个原生类型比较

Integer相互比较

integer i = 34; Integer j = 34;
Integer i = 34; 
比较的都是两个Integer对象,因此不涉及装箱和拆箱的操作
实质为:
System.out.println(Integer.IntegerCache.cache[i+128] == Integer.IntegerCache.cache[j]);
结果为false
//
integer i = 150; Integer j = 150;
实质为:
System.out.println(new Integer(i) == new Integer(j));
结果为false

int初始化默认为0
Integer初始默认是null

猜你喜欢

转载自www.cnblogs.com/INnoVationv2/p/12822558.html