Java中的装箱与拆箱

部分内容参考了
https://www.cnblogs.com/dolphin0520/p/3780005.html
装箱与拆箱是jdk5以后提供的功能
在jdk5之前

Integer i = new Integer(2);
Integer j = Integer.valueOf(3);

jdk5之后

Integer i = 2;
public class Main {
    public static void main(String[] args) {
        Integer i = 10;
        int n = i;
    }
}

这样一段代码实际上在编译后的字节码文件中
在这里插入图片描述

其实装箱就是调用了valueOf方法,拆箱就是调用xxxValue方法。
这里就会涉及到一个问题
两个包装类型 == 时 ,因为调用了valueOf方法这时 怎么判断

 public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }
 public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

这里涉及到一个类叫IntegerCache的类

private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];
        // 这里 能够指定 缓存的最大值  默认是127
        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;

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

其实就是判断在缓存里吗? 如果不在就new Integer 我们知道 new 关键字会在内存开辟一个新的内存空间 所以 == 会变成false

它能够设置上限 -Djava.lang.Integer.IntegerCache.high=256 这样他就不再是-128127了,而是-128256
但是Long.class的 valueOf

public static Long valueOf(long l) {
        final int offset = 128;
        if (l >= -128 && l <= 127) { // will cache
            return LongCache.cache[(int)l + offset];
        }
        return new Long(l);
    }

这个是不能 设置上下限的。
但是他还是有缓存的
对于浮点类型来说 Double.class Float.class都是直接new的 压根就没有缓存

 public static Double valueOf(String s) throws NumberFormatException {
        return new Double(parseDouble(s));
    }

再来看一个例子

  Integer i1 = 40;
  Integer i2 = 40;
  Integer i3 = 0;
  Integer i4 = new Integer(40);
  Integer i5 = new Integer(40);
  Integer i6 = new Integer(0);
  
  System.out.println("i1=i2 " + (i1 == i2));
  System.out.println("i1=i2+i3 " + (i1 == i2 + i3));
  System.out.println("i1=i4 " + (i1 == i4));
  System.out.println("i4=i5 " + (i4 == i5));
  System.out.println("i4=i5+i6 " + (i4 == i5 + i6));   
  System.out.println("40=i5+i6 " + (40 == i5 + i6));     

结果

i1=i2 true
i1=i2+i3 true
i1=i4 false
i4=i5 false
i4=i5+i6 true
40=i5+i6 true

可以看到 + 号后的结果 还是调用valueOf的结果
这里会有一点歧义 i4 == i5 + 16 i4其实是一个new 出来的Integer类型 i5 和 i6同样也是 Integer类型 那Integer类型 相加后 为什么和 new 出来的Integer类型相同呢?
这里就有意思了 i5+i6后会生成一个 int 类型的数字 而基本类型是可以直接 ==的 这就是 i4 = i5+ 16 为true的原因
下面我们来验证结果

        int i1 = 30;
        Integer i2 = new Integer(30);
        Integer i3 = new Integer(30);
        Integer i4 = 30;
        System.out.println("i1 = i2 " + (i1 == i2));
        System.out.println("i2 = i3 " + (i2 == i3));
        System.out.println("i3 = i4 " + (i3 == i4));
i1 = i2 true
i2 = i3 false
i3 = i4 false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值