java装箱拆箱

java装箱拆箱

装箱

基本数据类型 => 包装类

比如 int => Integer

拆箱

包装类 => 基本数据类型

比如 Integer => int

为什么会有基本数据类型?

因为,在java中new一个对象是存储在堆里的,对于 我们经常操作的数据类型,每次创建对象这样太消耗资源,因此java提供了8个基本数据类型,存储在栈里。用起来更方便。

自动类型转换
转换前的数据类型的位数低于转换后的数据类型。
例如: short数据类型的位数为16位,就可以自动转换位数为32的int类型,同样float数据类型的位数为32,可以自动转换为64位的double类型。

低 ----------------------------------------------------------> 高
byte,short,char—> int —> long—> float —> double
数据类型转换必须满足如下规则:
不能对boolean类型进行类型转换。
不能把对象类型转换成不相关类的对象。
由大到小会丢失精度:在把容量大的类型转换为容量小的类 型时必须使用强制类型转换。

基本数据类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

做个实验

做个简单的实验,输出一下各种情况下装箱和拆箱的对比的结果

 				int i1 = 132;
        Integer i2 = 132;
        System.out.printf("i1 == i2:%15b%n",(i1 == i2));
        System.out.printf("i2.equals(i1):%10b%n",i2.equals(i1));

        Integer i3 = 132;
        Integer i4 = 132;
        System.out.printf("i3 == i4:%15b%n",(i3 == i4));
        System.out.printf("i3.equals(i4):%10b%n",i3.equals(i4));

        Integer i5 = new Integer(132);
        Integer i6 = new Integer(132);
        System.out.printf("i5 == i6:%15b%n",(i5 == i6));
        System.out.printf("i5.equals(i6):%10b%n",i5.equals(i6));

        Integer i7 = 132;
        Integer i8 = new Integer(132);
        System.out.printf("i7 == i8:%15b%n",(i7 == i8));
        System.out.printf("i7.equals(i8):%10b%n",i7.equals(i8));
        System.out.println("-----------------------------");
        int j1 = 32;
        Integer j2 = 32;
        System.out.printf("j1 == j2:%15b%n",(j1 == j2));
        System.out.printf("j2.equals(j1):%10b%n",j2.equals(j1));

        Integer j3 = 32;
        Integer j4 = 32;
        System.out.printf("j3 == j4:%15b%n",(j3 == j4));
        System.out.printf("j3.equals(j4):%10b%n",j3.equals(j4));

        Integer j5 = new Integer(32);
        Integer j6 = new Integer(32);
        System.out.printf("j5 == j6:%15b%n",(j5 == j6));
        System.out.printf("j5.equals(j6):%10b%n",j5.equals(j6));

        Integer j7 = 32;
        Integer j8 = new Integer(32);
        System.out.printf("j7 == j8:%15b%n",(j7 == j8));
        System.out.printf("j7.equals(j8):%10b%n",j7.equals(j8));

结果:

i1 == i2:           true
i2.equals(i1):      true
i3 == i4:          false
i3.equals(i4):      true
i5 == i6:          false
i5.equals(i6):      true
i7 == i8:          false
i7.equals(i8):      true
-----------------------------
j1 == j2:           true
j2.equals(j1):      true
j3 == j4:           true
j3.equals(j4):      true
j5 == j6:          false
j5.equals(j6):      true
j7 == j8:          false
j7.equals(j8):      true

可以发现,equals比较均为true,探究原因,下面是Integer.equals源码
在这里插入图片描述

这里就要说说这个intValue了,这个方法源码很简单,就是把这个对象中的基本数据类型返回

在这里插入图片描述

简而言之,Integer.equals是值比较,是int和int之间的比较

再看 == 的比较

==的本质是内存地址的比较

Integer == int判断时,会将Ineger拆箱,转为基本数据类型,这是就是int 和 int的比较,转变成了值的比较

Integer == Integer判断时,是内存地址的比较,或者说是引用间的比较

继续对比分割线上下结果,可以发现上下这里有区别

				Integer i3 = 132;
        Integer i4 = 132;
        System.out.printf("i3 == i4:%15b%n",(i3 == i4));
        System.out.printf("i3.equals(i4):%10b%n",i3.equals(i4));
        
        Integer j3 = 32;
        Integer j4 = 32;
        System.out.printf("j3 == j4:%15b%n",(j3 == j4));
        System.out.printf("j3.equals(j4):%10b%n",j3.equals(j4));
i3 == i4:          false
i3.equals(i4):      true
----------------------------
j3 == j4:           true
j3.equals(j4):      true

这里首先注意这里

Integer i3 = 132;
Integer i4 = 132;

左边是包装类型,右边是基本数据类型

这里Integer会做 自动装箱 处理,自动将基本数据类型转变为包装类型

这里自动装箱会自动调用Integer.valueOf()方法

继续放源码

在这里插入图片描述

可以看到,这里并不是一定会new一个新的包装类型,而是先检查i是否在缓存范围,如果再就用缓存中的对象

继续探究,low和high的值是什么?

直接上源码

    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;

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

可以看到low固定为-127,high默认为127,可以使用jvm参数(java.lang.Integer.IntegerCache.high)设置high的值

下面还有一个有意思的地方

private IntegerCache() {}

私有构造方法:私有的构造方法无法在本类外部使用,也就导致本类无法用new实例化,这样可以控制对象的生成。

所以,i3和i4是两个包装类型对象(他们内存地址不同)

由于==是内存地址比较

所以i3 == i4返回时false

咱们再看分割线以下的部分

这里j3 == j4返回为true

上面已经提到了,Integer内部有缓存池,由于32在缓存池范围内,所以会返回同样的缓存池中的对象

所以,j3 == j4返回为true

继续看这个比较

 				Integer j5 = new Integer(32);
        Integer j6 = new Integer(32);
        System.out.printf("j5 == j6:%15b%n",(j5 == j6));
        System.out.printf("j5.equals(j6):%10b%n",j5.equals(j6));
i5 == i6:          false
i5.equals(i6):      true

这里就很好理解了,地址不同,值相同的两个对象

j5 == j6自然为false

最后一个比较

				Integer j7 = 32;
        Integer j8 = new Integer(32);
        System.out.printf("j7 == j8:%15b%n",(j7 == j8));
        System.out.printf("j7.equals(j8):%10b%n",j7.equals(j8));
j7 == j8:          false
j7.equals(j8):      true

这里返回false,原因:

j7为一个自动装箱的实例,最终结果为缓存池中的Integer对象,值为32,

j8为一个新new的对象

j7和j8是两个地址不同,值相同的两个对象

故返回false



如有误,请指正

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值