详解Java 自动装箱与自动拆箱

2025-05-29 0 71

包装器

有些时候,我们需要把类似于int,double这样的基本数据类型转成对象,于是设计者就给每一个基本数据类型都配置了一个对应的类,这些类被称为包装器。

包装器整体来说分为四大种:

  1. Number,Number类派生出了Integer,Double,Long,Float,Short,Byte这六个小类分别代表了int,double,long,float,short,byte这六种基本数据类型。
  2. Character,对应的基本数据类型是char。
  3. Void,对应的是关键字void,这个类我们会经常在反射中看到,用于表示方法的返回值是void,这里不再赘述,后面反射章节详细讲解。
  4. Boolean,对应的是基本数据类型boolean。

要记住下面两点包装器的特性:

包装器是不可变的,一旦构造了包装器,就不允许更改包装在其中的值。

  1. 包装器是final定义的,不允许定义它的子类。

自动装箱和自动拆箱

?

1

2

3

4

5
ArrayList<Integer> list = new ArrayList<>();

list.add(3);

int x = list.get(0);

自动装箱

当我们添加int值 到一个集合元素全部是Integer的集合中去时候,这个过程发生了什么?

?

1

2

3

4
list.add(3);

//实际上面的代码会被编译器给自动的变成下面的这个代码

list.add(Integer.valueOf(3))

编译器在其中所作的这个事情就叫做自动装箱

自动拆箱

当我们取出一个集合中的元素并将这个元素赋给一个int类型的值的时候,这其中又发生了什么呢?

?

1

2

3

4
int x = list.get(0);

//实际上面的代码会被编译器给自动的变成下面的这个代码

int x = list.get(0).intValue();

编译器这其中所作的这个事情就叫做自动拆箱

自动装箱和自动拆箱中的坑

?

1

2

3

4

5

6

7
Integer i1 = 100;

Integer i2 = 100;

Integer i3 = 300;

Integer i4 = 300;

System.out.println(i1 == i2);

System.out.println(i3 == i4);

这是一道经典的面试题,打印出来的结果是:

true
false

为什么会发生这样的事情,我们记得自动装箱的时候会自动调用Integer的valueOf方法,我们现在来看一下这个方法的源码:

?

1

2

3

4

5
public static Integer valueOf(int i) {

if (i >= IntegerCache.low && i <= IntegerCache.high)

return IntegerCache.cache[i + (-IntegerCache.low)];

return new Integer(i);

}

而这个IntegerCache是什么呢?

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

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

}

从这2段代码可以看出,在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。

上面的代码中i1和i2的数值为100,因此会直接从cache中取已经存在的对象,所以i1和i2指向的是同一个对象,而i3和i4则是分别指向不同的对象。

这样我们就不难理解为什么一个是false,一个是true了。

其他的包装器的valueOf方法也有不同的实现和不同的范围,具体的我们会在源码深度解析专栏来分析,敬请期待~

以上就是详解Java 自动装箱与自动拆箱的详细内容,更多关于Java 自动装箱与自动拆箱的资料请关注快网idc其它相关文章!

原文链接:https://cloud.tencent.com/developer/article/1385427

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解Java 自动装箱与自动拆箱 https://www.kuaiidc.com/117580.html

相关文章

发表评论
暂无评论