前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >两个数值相等的Integer不一定相等,为什么

两个数值相等的Integer不一定相等,为什么

作者头像
PhoenixZheng
发布2018-08-07 16:18:19
9850
发布2018-08-07 16:18:19
举报
文章被收录于专栏:Phoenix的Android之旅

昨天说到两个值是128的 Integer 对象 用 == 来比较的话结果是 false, 今天解释下为什么

== 原理

看了昨天的文章的朋友应该明白, == 其实是对内存地址的比较, 对于这段结果为 false 的代码来说,

代码语言:javascript
复制
public class Demo {
    public static void main(String[] args) {
      Integer a = 128;
      Integer b = 128;
      System.out.println("result: " + (a == b));
    }
}

false的结果只有一个可能性, 就是两个的内存地址不一样。

现在我们来稍微修改一下 Integer的值,变成127再看看,

代码语言:javascript
复制
public class Demo {
    public static void main(String[] args) {
      Integer a = 127;
      Integer b = 127;
      System.out.println("result: " + (a == b));
    }
}

$ java Demo result: true

晕了么, 为啥这里的内存地址又一样了?

Integer对数据的处理逻辑

== 在对基本类型进行比较的时候一切都还好, 还能按照预期的结果运行, 但如果对复合类型的话就有点不一样了。

对于 Integer来说, 范围在 -128 ~ 127 的数, 在内存中会有个缓冲数组用,存了对应的数, 每次使用的时候只是从其中拿对应的 Integer对象出来复用而已, 所以 == 对于在这个范围的 Integer来说, 只要值相同,内存地址就是相同的。

而超出这个范围的话, 当然内存地址也就不同了。

如果你看过 Integer的源码就明白了, 贴一下简单的源码,

代码语言:javascript
复制
/**
 * Cache to support the object identity semantics of autoboxing for values between
 * -128 and 127 (inclusive) as required by JLS.
 *
 * The cache is initialized on first usage.  The size of the cache
 * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
 * During VM initialization, java.lang.Integer.IntegerCache.high property
 * may be set and saved in the private system properties in the
 * sun.misc.VM class.
 */

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

IntegerCache 就是内存中用来存放 -128~127的缓存对象了 如果我们挖深一层,看看在初始化 Integer对象的时候都发生了什么, 就更能明白其中的逻辑了。 比如上面那段 127 的例子代码, 我们把它的字节码拿出来看一下,

代码语言:javascript
复制
public class Demo {
  public Demo();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: bipush        127
       2: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       5: astore_1
       6: bipush        127
       8: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      11: astore_2
....
}

这里很明显可以看到, Integer a = 127, 其实编译器是调用了 Integer.valueOf(127)来实现, 而 valueOf() 的逻辑是,

代码语言:javascript
复制
public static Integer valueOf(int i) {
   if (i >= IntegerCache.low && i <= IntegerCache.high)
       return IntegerCache.cache[i + (-IntegerCache.low)];
   return new Integer(i);
}

看到了么, 如果落在 IntegerCache.low 到 IntegerCache.high 范围的数, 是直接从 cache中拿的。

总结

对于 Integer a = %d 来说, 在 -128 ~ 127 范围的数的比较,== 没什么问题, 因为缓存池的存在,这里比较的是相同的内存地址, 但当超过这个范围的话,用 == 来比较相同值的 Integer 对象是错误的, 因为在这个范围外的 Integer 对象有独立的内存地址, 只能用 equals()来比较。

当然如果在初始化 -128 ~ 127 范围的 Integer对象的时候, 如果用的是 new Integer()构造方法的话, 也会分配不同的内存对象, 这时候 == 也是不行的。

所以在项目中为了谨慎起见, 对于非基本类型外的对象, 尽量用 equals()来比较才是明智的选择。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-05-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Android每日一讲 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • == 原理
  • Integer对数据的处理逻辑
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档