为什么Integer用双等号比较时127相等而128不相等

为什么Integer用==比较时127相等而128不相等?

引用:https://zhuanlan.zhihu.com/p/368421654

一、问题引出

1
2
3
4
5
6
7
public static void main(String[] args) {
for (int i = 0; i < 150; i++) {
Integer a = i;
Integer b = i;
System.out.println(i + " " + (a == b));
}
}

i取值从0到150,每次循环a与b的数值均相等,输出a == b。运行结果:

1
2
3
4
5
6
7
8
9
10
11
0 true
1 true
2 true
3 true
...
126 true
127 true
128 false
129 false
130 false
...

从128开始a和b就不再相等了

二、自动装箱

首先回顾一下自动装箱。对于下面这行代码

1
Integer a = 1;

变量a为Integer类型,而1为int类型,且Integer和int之间并无继承关系,按照Java的一般处理方法,这行代码应该报错。

但因为自动装箱机制的存在,在为Integer类型的变量赋int类型值时,Java会自动将int类型转换为Integer类型,即

1
Integer a = Integer.valueOf(1);

valueOf()方法返回一个Integer类型值,并将其赋值给变量a。这就是int的自动装箱。

三、原理分析

每次循环时,Integer a = i和Integer b = i都会触发自动装箱,而自动装箱会将int转换Integer类型值并返回;我们知道Java中两个new出来的对象因为是不同的实例,无论如何==都会返回fasle。比如:

1
new Integer(1) == new Integer(1);

就会返回false。

那么例子中Integer a = i和Integer b = i自动装箱产生的变量a和b就不应该时同一个对象了,那么==的结果应该时false。

128以上为false容易理解,但为何0到127时返回true了呢?==返回true的唯一情况是比较的两个对象为同一个对象,那不妨把例子中a和b的内存地址都打印出来看看:

1
2
3
4
5
for(int i=0;i<150;i++){
Integer a=i;
Integer b=i;
System.out.println(a+" "+b+" "+System.identityHashCode(a)+" "+System.identityHashCode(b));
}

identityHashCode()方法可以理解为输出对应变量的内存地址,输出为:

1
2
3
4
5
6
7
8
9
10
11
0 0 762119098 762119098
1 1 1278349992 1278349992
2 2 1801910956 1801910956
3 3 1468253089 1468253089
...
126 126 1605164995 1605164995
127 127 1318497351 1318497351
128 128 101224864 479240824
129 129 1373088356 636728630
130 130 587071409 1369296745
...

竟然从0到127不同时候自动装箱得到的是同一个对象!从128开始才是正常情况。

四、源码分析

“从0到127不同时候自动装箱得到的是同一个对象”就只能有一种解释:自动装箱并不一定new出新的对象。

既然自动装箱涉及到的方法是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);
}

如果int型参数i在IntegerCache.low和IntegerCache.high范围内,则直接由IntegerCache返回;否则new一个新的对象返回。IntegerCache.low就是-128,IntegerCache.high就是127

在IntegerCache的static块中就一次性生成了-128到127直接的Integer类型变量存储在cache[]中,对于-128到127之间的int类型,返回的都是同一个Integer类型对象。

这下真相大白了,整个工作过程就是:Integer.class在装载(Java虚拟机启动)时,其内部类型IntegerCache的static块即开始执行,实例化并暂存数值在-128到127之间的Integer类型对象。当自动装箱int型值在-128到127之间时,即直接返回IntegerCache中暂存的Integer类型对象。

为什么Java这么设计?我想是出于效率考虑,因为自动装箱经常遇到,尤其是小数值的自动装箱;而如果每次自动装箱都触发new,在堆中分配内存,就显得太慢了;所以不如预先将那些常用的值提前生成好,自动装箱时直接拿出来返回。哪些值是常用的?就是-128到127了。


为什么Integer用双等号比较时127相等而128不相等
http://example.com/2025/05/14/为什么Integer用双等号比较时127相等而128不相等/
作者
Kon4tsu
发布于
2025年5月14日
许可协议