Java-自动装箱和拆箱 基本类型与包装类的区别

什么是装箱?什么是拆箱?

自动装箱就是Java自动将基本类型值转化为对应的对象,
将int类型的变量转换为Integer对象,这个过程叫做装箱
反之将Integer对象转换为int类型值,这个过程叫做拆箱

Integer i = 10;  //装箱
int n = i;   //拆箱
箱和拆箱是如何实现的
  • 装箱的时候自动调用的是Integer的valueOf(int)方法

  • 拆箱的时候自动调用的是Integer的intValue()方法

基本类型与包装类的区别:
  • 包装类是对象,基本数据类型不是对象
  • 声明方式不同,封装类需要用new关键字来创建,基本数据类型不需要new
  • 存储方式及位置不同,基本数据类型是直接存储变量的值包存在栈中,而封装类需要通过引用指向实例,具体的实例保存在堆里。
  • 初始值不同,封装类的初始值为null,基本数据类型的初始值视具体类型而定,如int初始值为0
  • 使用方式不同,如集合类只能使用包装类
面试中相关的问题

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

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() {}
    }
如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;
否则创建一个新的Integer对象。

Byte Long 都有cache范围都是[-128, 127]
public class Main {
    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 static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

在Boolean中定义了2个静态成员属性
public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);
Integer i = new Integer(xxx)
Integer i = xxx;

1)第一种方式不会触发自动装箱的过程;而第二种方式会触发;

2)在执行效率和资源占用上的区别。
第二种方式的执行效率和资源占用在一般性情况下要优于第一种情况(注意这并不是绝对的)。
public class Main {
    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;
         
        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));
    }
}

true
false
true
true
true
false
true
  • 当 "=="运算符的两个操作数都是 包装器类型的引用,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)。
  • 对于包装器类型,equals方法并不会进行类型转换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值