java两个integer数据判断相等用==还是equals

作者:lm 时间:2021-06-14 00:46:52 

目录
  • 问题案例

  • 原因分析

  • 源码分析

  • 解决方法

  • 备注

问题案例

来个简单点的例子


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。运行结果:

0 true
1 true
2 true
3 true
...
126 true
127 true
128 false
129 false
130 false
...

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

原因分析

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


Integer a = 1;

变量a为Integer类型,而1为int类型,且Integer和int之间并无继承关系,按照Java的一般处理方法,这行代码应该报错。
但因为自动装箱机制的存在,在为Integer类型的变量赋int类型值时,Java会自动将int类型转换为Integer类型,即


Integer a = Integer.valueOf(1);

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


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

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


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

就会返回false。

那么例子中Integer a = i和Integer b = i自动装箱产生的变量a和b就不应该时同一个对象了,那么==的结果应该时false。128以上为false容易理解,但为何0到127时返回true了呢?==返回true的唯一情况是比较的两个对象为同一个对象,那不妨把例子中a和b的内存地址都打印出来看看:


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()方法可以理解为输出对应变量的内存地址,输出为:

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(),不妨看看其源代码:


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

其注释里就直接说明了-128到127之间的值都是直接从缓存中取出的。看看是怎么实现的:如果int型参数i在IntegerCache.low和IntegerCache.high范围内,则直接由IntegerCache返回;否则new一个新的对象返回。似乎IntegerCache.low就是-128,IntegerCache.high就是127了
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() {}
}

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

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

解决方法

既然我们的目的是比较数值是否相等,而非判断是否为同一对象;而自动装箱又不能保证同一数值的Integer一定是同一对象或一定不是同一对象,那么就不要用==,直接用equals()好了。实际上,Integer重写了equals()方法,直接比较对象的数值是否相等。


for (int i = 0; i < 150; i++) {
   Integer a = i;
   Integer b = i;
   System.out.println(i + " " + (a.equals(b)));
}
//这样返回值就全都是true了。

private final int value;

public boolean equals(Object obj) {
   if (obj instanceof Integer) {
       return value == ((Integer)obj).intValue();
   }
   return false;
}

public int intValue() {
   return value;
}

备注

不仅int,Java中的另外7中基本类型都可以自动装箱和自动拆箱,其中也有用到缓存。见下表:

基本类型装箱类型取值范围是否缓存缓存范围
byteByte-128 ~ 127-128 ~ 127
shortShort-2^15 ~ (2^15 - 1)-128 ~ 127
intInteger-2^31 ~ (2^31 - 1)-128 ~ 127
longLong-2^63 ~ (2^63 - 1)-128~127
floatFloat----
doubleDouble----
booleanBooleantrue, falsetrue, false
charCharacter\u0000 ~ \uffff

来源:https://juejin.cn/post/7044419605997027336

标签:java,integer,判断相等
0
投稿

猜你喜欢

  • java计算工作时间除去节假日以及双休日

    2023-09-24 20:34:58
  • Maven继承与聚合详解及作用介绍

    2023-03-08 00:14:36
  • 浅谈Java锁的膨胀过程以及一致性哈希对锁膨胀的影响

    2023-06-03 15:03:38
  • 详解如何在Java中实现堆排序算法

    2023-11-11 11:34:46
  • SpringMVC实现上传下载文件

    2022-11-10 03:18:14
  • 详解Java的TCP/IP编程学习--基于定界符的成帧

    2023-11-23 03:02:32
  • Java如何实现定时任务

    2021-11-07 02:57:38
  • Java反射通过Getter方法获取对象VO的属性值过程解析

    2023-04-11 06:11:33
  • spring与mybatis三种整合方法

    2021-11-27 22:00:46
  • Java数组越界问题实例解析

    2023-10-25 18:16:23
  • Java实现打字游戏

    2021-11-02 16:46:56
  • 关于@Autowired注入依赖失败的问题及解决

    2023-11-29 00:50:42
  • java使用ffmpeg处理视频的方法

    2023-07-28 22:58:08
  • Spring Cloud Config RSA简介及使用RSA加密配置文件的方法

    2023-11-28 22:39:26
  • MybatisPlus实现简单增删改查功能

    2021-12-27 06:25:21
  • IntelliJ IDEA中查看文件内所有已声明的方法(类似eclipse的outline)

    2021-08-06 00:39:39
  • Scala中的mkString的具体使用方法

    2023-11-16 00:18:18
  • spring+srpingmvc+hibernate实现动态ztree生成树状图效果

    2022-07-21 21:41:59
  • spring-boot读取props和yml配置文件的方法

    2022-06-01 14:54:26
  • Android使用Gradle依赖配置compile、implementation与api的区别介绍

    2023-09-27 18:11:40
  • asp之家 软件编程 m.aspxhome.com