我所理解的JDK自动装箱和拆箱

1,什么是自动装箱和拆箱?
Java中有8种基本数据类型:1字节=8bit(位)

  • 整形:byte(1字节) short(2字节) int(4字节) long(8字节)
  • 浮点型:float(4字节) double(8字节)
  • 字符型:char(2字节)
  • 布尔型boolean(JVM未规定,Hospot采用4字节)

对应的包装类型为:Byte、Short、Integer、Long、Float、Double、Character、Boolean

包装类型的对象存在与堆空间中。基本数据类型的存在是为了更有效率地方便计算。

  JDK1.5之前,这两种类型不能直接转换。从JDK1.5开始,为了方便开发,开始支持这两种类型直接转换。从基本数据类型到包装类型称为装箱,从包装类型到基本数据类型称为拆箱

Integer i = 3; // 自动装箱
int a = i; // 自动拆箱

2,自动装箱和拆箱的源码实现
先看int和Integer的转换:

Integer类有个属性: private final int value;

自动拆箱会调用Integer.intValue(),源码:

    public int intValue() {
        return value;
    }

自动装箱会调用Integer.valueOf(),源码:

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high) // 如果i在IntegerCache.low和IntegerCache.high的范围内
            return IntegerCache.cache[i + (-IntegerCache.low)]; //返回IntegerCache.cache[]中的Integer数据
        return new Integer(i); // 如果不在缓存范围内,新建一个Integer对象
    }

IntegerCache是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"); // 可以通过启动参数-Djava.lang.Integer.IntegerCache.high=127来指定
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    // 最大值不能小于127
                    i = Math.max(i, 127);
                    // 最大值不能超过Integer.MAX_VALUE=2147483647
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // 如果指定的参数不能转换为int类型数据,直接跳过。
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++) // 为缓存数组赋值
                cache[k] = new Integer(j++);

            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

  其他的整形类型和字符型与此类似,均有缓存。Byte,Short,Long 有固定范围: -128 到 127。对于 字符型Character, 范围是 0 到 127。
  除了 Integer 可以通过参数改变范围外,其它的都不行。
  自动装箱时如果是缓存边界之外的数据,则新创建对象,否则返回缓存中的对象
  浮点型数据没有缓存。(可能因为浮点型数据没有范围。。。)

布尔型Boolean的装箱和拆箱源码: Boolean类有3个属性:

public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);
private final boolean value;

自动装箱的源码:

    public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

自动拆箱的源码:

    public boolean booleanValue() {
        return value;
    }

参考资料:

  1. 以上内容为笔者日常琐屑积累,已无从考究引用。如果有,请站内信提示。

转载于:https://my.oschina.net/u/3466682/blog/1581964

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值