Java拆装箱深度剖析

2025-05-29 0 46

先来看一段代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13
public class Main{

public static void main(String[] args){

Integer num1 = 100;

Integer num2 = 100;

Integer num3 = 200;

Integer num4 = 200;

'''//输出结果'''

System.out.println(num1==num2);

System.out.println(num3==num4);

}

}

猜猜结果是什么?

很多人都会认为结果全为true,但结果去不是这样的

true
false

为什么是这样的结果?如果用内存来解释结果的话,num1和num2指向的是同一个对象,而num3和num4则指向的确是不同的对象。接下来就告诉你为什么,看一看Integer类型的valueof方法的源码:

?

1

2

3

4

5

6
public static Integer valueOf(int i) {

assert IntegerCache.high >= 127;

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

return IntegerCache.cache[i + 128];

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
'''// IntegerCache,一个内部类,注意它的属性都是定义为static final'''

private static class IntegerCache {

static final int high; '''//缓存上界,暂为null'''

static final Integer cache[]; '''//缓存的整型数组'''

'''// 块,为什么定义为块'''

static {

final int low = -128; '''// 缓存下界,不可变了。只有上界可以改变'''

'''// high value may be configured by property'''

'''// h值,可以通过设置jdk的AutoBoxCacheMax参数调整(以下有解释),自动缓存区间设置为[-128,N]。注意区间的下界是固定'''

int h = 127;

if (integerCacheHighPropValue != null) {

'''// Use Long.decode here to avoid invoking methods that'''

'''// require Integer's autoboxing cache to be initialized'''

// 通过解码integerCacheHighPropValue,而得到一个候选的上界值'''

int i = Long.decode(integerCacheHighPropValue).intValue();

'''// 取较大的作为上界,但又不能大于Integer的边界MAX_VALUE'''

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++); '''// -128到high值逐一分配到缓存数组'''

}

private IntegerCache() {}

}

通过这两段代码可以看出,在通过valueof方法创建Integer类型对象时,取值范围为[-128,127],数值在这个区间里,指针指向IntegerCache.cache中已经存在的对象引用,当数值超出这个范围,就会创建一个新的对象。

有一点需要注意的是,并不是所有的类型都是这个范围,看Double类型:

?

1

2

3

4

5

6

7

8

9

10

11

12
public class Main{

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

具体为什么回事这样的结果,大伙可以去看看源代码中Double valueof方法的实现,其和Integer valueof方法不同,是因为在某个范围内的整型数值的个数是有限的,而浮点数却不是。

注意,Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的。
Double、Float的valueOf方法的实现是类似的。

拉下了一个,Boolean类型的结果有两个True or False。直接看源代码:

?

1

2

3
public static Boolean valueOf(boolean b) {

return (b ? TRUE : FALSE);

}

而其中的TRUE和FALSE是这样定义的:

?

1

2

3

4

5

6

7
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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 Java拆装箱深度剖析 https://www.kuaiidc.com/119566.html

相关文章

发表评论
暂无评论