装箱与拆箱

一、什么是装箱?什么是拆箱?

装箱 自动将基本数据类型转换为包装器类型

Integer i = 10;

拆箱 自动将包装器类型转换为基本数据类型。

int n = i;

注:装箱的过程会创建对应的对象,会消耗内存,所以装箱的过程会增加内存的消耗,影响性能。

基本数据类型包装器类型
int(4字节)Integer
byte(1字节)Byte
short(2字节)Short
long(8字节)Long
float(4字节)Float
double(8字节)Double
char(2字节)Character
boolean(未定)Boolean

二、为什么需要装箱与拆箱?

基本数据类型不是对象,需要装箱才能把数据类型变成一个类,那就可以把装箱过后的基本数据类型当做一个对象,就可以调用object子类的接口。而且基本数据类型是不可以作为形参使用的,装箱后就可以。而且在jdk1.5之后就实现了自动装箱拆箱,包装数据类型具有许多基本数据类型不具有的功能,只是装箱拆箱过程会稍稍微的影响一下效率

三、源码解析

装箱源码解析

public static Integer valueOf(int i) {
        if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
    }

如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。

private static class IntegerCache {
        static final int high;
        static final Integer cache[];
 
        static {
            final int low = -128;
 
            // high value may be configured by property
            int h = 127;
            if (integerCacheHighPropValue != null) {
                // Use Long.decode here to avoid invoking methods that
                // require Integer's autoboxing cache to be initialized
                int i = Long.decode(integerCacheHighPropValue).intValue();
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - -low);
            }
            high = h;
 
            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }
 
        private IntegerCache() {}
    }

首先做的就是为high赋值,因为声明的时候只给low赋值了,然后通过sun.misc.VM.getSavedProperty(“java.lang.Integer.IntegerCache.high”)获取一个值,这个值可以去配置修改,这个我们不做过多解释,假定配置文件的值是默认的,也就是最终high取值127;数组cache开辟了地址空间,大小为high-low+1,然后再从0下标开始初始化对应的值从low开始(也就是-128)递增至结束。程序会将这个值的引用存在缓存区中。如果所以给定的基本类型int值在-128到127之间的话,就会直接去cache数组里取。

注意:Integer、Short、Byte、Character、Long这几个类的valueOf方法的 实现是类似的。Double、Float的valueOf方法的实现是类似的。
Double的valueOf方法的实现如下:
public static Double valueOf(double d) {
        return new Double(d);
    }

这里还有Boolean的没有看,其实很简单,我们看一下源码
public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

上面代码中的‘TRUE’是一个对象

public static final Boolean TRUE = new Boolean(true);
	 /** 
     * The <code>Boolean</code> object corresponding to the primitive 
     * value <code>false</code>. 
     */
public static final Boolean FALSE = new Boolean(false);

拆箱源码解析

 public int intValue() {
        return value;
    }

这个value值是一个内部变量,在声明Integer对象的时候用的,这里直接返回即可得到拆箱后的结果。

四、练习

当 "=="运算符的两个操作数都是包装器类型的引用,则是比较指向的是否是同一个对象;如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)。另外,对于包装器类型,equals方法并不会进行类型转换。
public class Test {
    public static void main(String[] args) {
    
        Integer i1 = 100;
        Integer i2 = 100;
        Integer i3 = 200;
        Integer i4 = 200;
         
        System.out.println(i1==i2);
        System.out.println(i3==i4);
    }
}

结果:true;false

public class Test {
    public static void main(String[] args) {
         
        Double i1 = 100.0;
        Double i2 = 100.0;
        Double i3 = 200.0;
        Double i4 = 200.0;
         
        System.out.println(i1==i2);
        System.out.println(i3==i4);
    }
}

结果:false;false

public class Test {
    public static void main(String[] args) {
         
        Boolean i1 = false;
        Boolean i2 = false;
        Boolean i3 = true;
        Boolean i4 = true;
         
        System.out.println(i1==i2);
        System.out.println(i3==i4);
    }
}

结果:true;true

public class Test {
    public static void main(String[] args) {
         
        Integer a = 1;
        Integer b = 2;
        Integer c = 3;
        Integer d = 3;
        Integer e = 321;
        Integer f = 321;
        Long g = 3L;
        Long h = 2L;
        Integer i4=new Integer(10);
        Integer i5=new Integer(10);
        Integer i6=new Integer(500);
        Integer i7=new Integer(500);
         
        System.out.println(c==d);
        System.out.println(e==f);
        System.out.println(c==(a+b));
        System.out.println(c.equals(a+b));
        System.out.println(g==(a+b));
        System.out.println(g.equals(a+b));
        System.out.println(g.equals(a+h));
        System.out.println(i4==i5);
        System.out.println(i6==i7);
    }
}

结果:true;false;true;true;true;false;true;false;false

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值