自动装箱 自动拆箱

自动装箱 自动拆箱是JDK 1.5的新特性。
说道自动装箱和拆箱,首先我们需要知道基本数据类型包装类
java提供了四类八种基本数据类型,但是有些需求,靠基本数据类型无法满足我们的需求,或者是不方便。例如做一些进制转换获取int数据类型的取值范围等等。
我们知道,类的优点在于,它可以定义成员变量、成员方法,提供丰富便利的功能。因此Java在JDK1.0的时候就设计了基本数据类型的包装类。对应关系如下图:
在这里插入图片描述
.
自动装箱:把基本数据类型转换为包装类类型。
自动拆箱:把包装类类型转换为基本数据类型。

我们都知道,Java数据类型分两种:基本数据类型 和 引用数据类型(对象)
有时候我们需要将基本数据类型包装为对象进行处理
我们使用eclipse创建一个java项目,版本兼容性选择jdk-1.4
在这里插入图片描述
这种情况就会报错,提示Type mismatch: cannot convert from int to Integer类型不匹配:不能从int转换为Integer,
需要这么写

public class Test {
	
	//int 转换为 Integer
	int i = 10;
	Integer integer = new Integer(i);

	//Integer 转换为 int
	Integer integer1 = new Integer(100);
	int i1 = integer1.intValue();
}

jdk1.5起,引入了自动装箱自动拆箱的新特性
就可以像下面这么写

//int 转换为 Integer
Integer integer = 10;

//Integer 转换为 int
int i = integer;

以整型为例
观察代码实现过程

在这里插入代码片

打断点,右键,Debug As 运行,F5进入该行函数内部,就会看到如下代码

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

Ctrl+鼠标左键进入IntegerCache类,代码如下

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=-128,这里通过反射的方式将高值配置成127

 int h = 127;
 String integerCacheHighPropValue =sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");

注释
high value may be configured by property告诉我们高值可以通过配置更改
也即执行了如下代码

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

注意:计算机做整形数据的加减法,是用int计算的,其实最底层是靠int数据的二进制的补码做加减法的。
Integer与int类型的装箱和拆箱,分别是通过Integer.valueOf()和Integer.intValue()实现的。

注意:在使用拆装箱操作的时候,需要注意判断对象不要为null,否者java.lang.NullPointerException

IntegerCache有静态成员变量cache,为一个拥有256个元素的数组。在IntegerCache中也对cache进行了初始化,即第i个元素是值为i-128的Integer对象。称作缓存区

1、基本型和基本型封装型进行“==”运算符的比较,基本型封装型将会自动拆箱变为基本型后再进行比较,因此Integer(0)会自动拆箱为int类型再进行比较,显然返回true;

     int a = 220;

     Integer b = 220;

    System.out.println(a==b);//true

2、两个Integer类型进行“==”比较, 如果其值在-128至127 ,那么返回true,否则返回false, 这跟Integer.valueOf()的缓冲对象有关,这里不进行赘述。

    Integer c=3;

    Integer h=3;

    Integer e=321;

    Integer f=321;

    System.out.println(c==h);//true

    System.out.println(e==f);//false

3、两个基本型的封装型进行equals()比较,首先equals()会比较类型,如果类型相同,则继续比较值,如果值也相同,返回true。

    Integer a=1;

    Integer b=2;

    Integer c=3;

    System.out.println(c.equals(a+b));//true

4、基本型封装类型调用equals(),但是参数是基本类型,这时候,先会进行自动装箱,基本型转换为其封装类型,再进行3中的比较。

    int i=1;

    int j = 2;

    Integer c=3;

    System.out.println(c.equals(i+j));//true
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值