装箱和拆箱,128陷阱

本文详细介绍了Java中基本类型与包装类之间的转换,包括装箱和拆箱操作。通过Integer.valueOf()方法的源码分析,揭示了对于[-128,127]之间的数值,Integer对象会复用缓存,避免了重复创建。同时,文章讨论了“128陷阱”,即当两个不同包装类实例的值相等但超出缓存范围时,它们的引用并不相同。这有助于理解Java内存模型和对象创建的细节。
摘要由CSDN通过智能技术生成

Java中每个基本类型都有一个包装类。例

byte → Byte

short → Short

int → Integer

long → Long

float → Float

double → Double

char → Character

boolean→ Boolean

基本类型转换为包装类类型:装箱

 //装箱
        int a = 100;
        Integer a1 = Integer.valueOf(a);
        System.out.println(a1);

源码分析:

//Integer包装类的方法
public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

如果传入的值在[-128,127]之间则,则不需要重新创建Integer对象。否则重新创建一个Integer对象。

IntegerCache是Integer的静态内部类,在static静态代码块中创建了一个范围为[-128,127],类型为Integer的数组,且进行了初始化操作。

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 =
                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 = h;

            //创建一个范围为[-128,127]大小,类型为Integer的数组
            //创建256个Integer类型的对象赋给数组元素

            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() {}
    }

包装类类型转换为基本类型:拆箱

//拆箱
        Integer b = 100;
        int b1 = b.intValue();
        System.out.println(b1);

源码:直接返回包装类中的value的值。

 public int intValue() {
        return value;
    }

128陷阱分析:

        Integer a = 100;
        Integer b = 200;

        int a1 = 100;
        int b1 = 200;

        Integer c = 100;
        Integer d = 200;

        System.out.println(a==a1);
        System.out.println(b==b1);

        System.out.println(a==c);
        System.out.println(b==d);

输出结果:

true
true
true
false

结果分析:

System.out.println(a==a1):   包装类a的value值为100,和a1的值相等。所以结果为true。同理,

System.out.println(b==b1);   的结果为true。

System.out.println(a==c):    a和c均为包装类,且值都为100,在[-128,127]之间,所以引用的均为IntegerCache类中cache数组中的对象,在初始化时已经创建好。结果为true。

 System.out.println(b==d):   b和d均为包装类,且值都为200,在[-128,127]之外,它们都会创建新的Integer对象。结果为false。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值