Java自动装箱/拆箱机制本质

1,321 阅读4分钟


前言

近期遇到一个于Java自动装箱/拆箱有关的问题,如下:

public class Test03 {
    public static void main(String[] args) {
        Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
        System.out.println(f1 == f2);
        System.out.println(f3 == f4);    // 输出是啥??
    }
}

SO EASY!?   答案 false, false  理由:Integer自动装箱,相当于 Integer f1 = new Integer(100), f2 = new Integer(100),f3......在堆内存里占用不同的对象空间,== 比较引用,所以false, false

然而结果是:


不急,下面来揭晓 (●'◡'●)

装箱来源

Java 是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本 数据类型,但是为了能够将这些基本数据类型当成对象操作,Java 为每一个基本数据类型都引入了对应的包装类型(wrapper class),int 的包装类就是 Integer, 从 Java 5 开始引入了自动装箱/拆箱机制,使得二者可以相互转换。 Java 为每个原始类型提供了包装类型:

  • 原始类型: boolean,char,byte,short,int,long,float,double 
  • 包装类型:Boolean,Character,Byte,Short,Integer,Long,Float, Double


装箱本质

当我们给一个 Integer 对象赋一个 int 值的时候,(如:Integer i = 198)会调用 Integer 类的静态方法 valueOf,如果看看 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);
}

这里 i 就是198,首先与 IntegerCache类(Integer内部静态类)里的low,high这两个属性比较,在这范围内就从cache里取出来,没有在范围内就直接返回一个新的在堆内存分配的Integer(198),那这个IntegerCache就是关键,因为没有if()...... 这段,那就是返回new Integer,== 比较肯定为false,下面来看看

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=} 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[];
    static {
// 先设定一个high值
        int h = 127;
// 下面 integerCacheHighPropValue是通过 JVM设置-XX:AutoBoxCacheMax=  获得的
        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属性赋值,即使high被static final修饰
        high = h;
// 给 cache 赋一个数组,并且从 low(-128)开始+1递增赋给数组,直到high(这里不设置VM的话就是127)
        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是Integer内部静态类,当JVM启动后,会自动加载java.lang下面的类,Integer类会被加载到方法区,之后是验证阶段,准备阶段时,会给static方法,static类(IntegerCache),static属性 分配空间,但不会赋值。然后常量池内加载IntegerCache类的  static final int low,static final int high, static final Integer cache[]

初始化阶段,给low赋值 -128,并且执行static代码块,static代码块里面会给high属性赋值,即使high被final修饰

看完IntegerCache后,回到ValueOf片段,从cache(位于常量池,-128 ~ 127)取值,应该就很清楚了 。简单的说,如果整型字面量的值在-128 到 127 之间,那么不会 new 新的 Integer 对象,而是直接引用常量池中的 Integer 对象  ( ̄︶ ̄)↗ 

if (i >= IntegerCache.low && i <= IntegerCache.high)    
        return IntegerCache.cache[i + (-IntegerCache.low)];


总结

回到原先的问题

public class Test03 {
    public static void main(String[] args) {
        Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
        System.out.println(f1 == f2);
        System.out.println(f3 == f4);    // 输出是啥??
    }
}

f1, f2装箱,100位于 -128 ~ 127之间,直接指向常量池的cache[100 + (--128)],所以 == 比较为true,f3, f4不在,所以直接在堆内存中分配Integer对象,== 为false


补充

再补充一个问题 (~ ̄▽ ̄)~

class AutoUnboxingTest {
    public static void main(String[] args) {
        Integer a = new Integer(3);
        Integer b = 3; // 将 3 自动装箱成 Integer 类型
        int c = 3;
        System.out.println(a == b); // false 两个引用没有引用同一对象
        System.out.println(a == c); // true a 自动拆箱成 int 类型再和c比较
    }
}

遇到Integer b = 3 这样的就要自动装箱,a指向堆内存某块,b指向常量池某块,==为false

注意: Integer类型与int基本类型比较,要拆箱,Integer转为int,值为3,== 是值比较,所以为true


希望对你有帮助 (o゚v゚)ノ