你有没有想过: 比较两个Integer,为什么必须使用equals?不用==

ゝ一纸荒年。 2022-12-11 13:46 161阅读 0赞

点击上方“java大数据修炼之道”,选择“设为星标”

" class="reference-link">优质文章和精品资源, 第一时间送达format_png

作者:码农的一天

原文:http://suo.im/5Wd5zB

equals和==的区别

  • 对于基本类型,比如int、long,进行判等,只能使用==,比较的是直接值,因为基本类型的值 就是其数值。
  • 对应用类型,比如Integer、Long和String,进行判等,需要使用equals进行内容判等。因为引用类型的直接值是指针,使用==的话,比较的是指针,也就是两个对象在内存中的地址,即比较它们是不是同一个对象,而不是比较对象的内容。

总结:比较值的内容,除了基本类型只能使用==外,其他类型都需要使用equals。

测试 Integer用例

1、使用==对两个值127的直接赋值的Integer对象判断

  1. Integer a = 127;
  2. Integer b = 127;
  3. System.out.println(a == b);//true

为什么结果是true呢?

编译器会把Integer a = 127转换为Integer.valudOf(127)。查看源码得知:

这个转换在内部其实做了缓存,是的两个Integer指向同一个对象,所以 == 返回true.

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

其中 IntegerCache.low = static final int low = -128;

IntegerCache.high = 127

2、使用 == 对两个值 为128的直接赋值的Integer对象判等

  1. Integer c = 128;
  2. Integer d = 128;
  3. System.out.println(c==d); //false

为什么会返回false呢?

默认情况下会缓存[-128,127]的数值,而128处于这个区间之外。

那么怎样让Integer缓存【-128,1000】呢?

可以设置JVM参数:-XX:AutoBoxCacheMax=1000

  1. private static class IntegerCache {
  2. static final int low = -128;
  3. static final int high;
  4. static final Integer cache[];
  5. static {
  6. // high value may be configured by property
  7. int h = 127;
  8. String integerCacheHighPropValue =
  9. sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
  10. if (integerCacheHighPropValue != null) {
  11. try {
  12. int i = parseInt(integerCacheHighPropValue);
  13. i = Math.max(i, 127);
  14. // Maximum array size is Integer.MAX_VALUE
  15. h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
  16. } catch( NumberFormatException nfe) {
  17. // If the property cannot be parsed into an int, ignore it.
  18. }
  19. }
  20. high = h;
  21. cache = new Integer[(high - low) + 1];
  22. int j = low;
  23. for(int k = 0; k < cache.length; k++)
  24. cache[k] = new Integer(j++);
  25. // range [-128, 127] must be interned (JLS7 5.1.7)
  26. assert IntegerCache.high >= 127;
  27. }
  28. private IntegerCache() {}
  29. }

3、使用==对一个127的直接赋值的Integer 和 另一个通过new Integer声明的值为127的对象判等

  1. Integer e = 127;
  2. Integer f = new Integer(127);
  3. System.out.println(e == f);//false

结果为false,为什么呢?

new 出来的Integer始终是不走缓存的新对象。比较一个新对象和一个来自缓存的对象,结果肯定不一样,因此返回false;

4、使用 == 对两个通过new Integer声明的 值为127的对象判等

  1. Integer g = new Integer(127);
  2. Integer h = new Integer(127);
  3. System.out.println(g == h);//false

结果为false,为什么呢?

new 出来的Integer始终是不走缓存的新对象。比较两个新对象,结果肯定不一样,因此返回false;

5、使用 == 对一个值为128的直接赋值的Integer对象和另一个职位128的int基本类型判等

  1. Integer i = 128;
  2. int j = 128;
  3. System.out.println(i == j);//true

结果为true,为什么呢?

Integer i = 128;是先拆箱再比较,比较的肯定是数值而不是引用,因此返回true。

总结:

只需要记得比较Integer的值请使用equals,而不是==,对于基本类型的int的比较当然只能使用==

Integer的误用

比如一个请假申请的状态,有这么一个枚举定义了请假的状态和对于状态的描述

  1. public enum ProcessEnum {
  2. POST(1,"已提交"),AGREE(2,"同意"),REJECT(3,"拒绝");
  3. private Integer status;
  4. private String desc;
  5. ProcessEnum( Integer status, String desc ) { this.status = status;
  6. this.desc = desc;
  7. }}

在业务代码中,使用了 == 对枚举和入参ProcessQuery中的status属性进行判等;

  1. public void compare(Integer status){
  2. ProcessEnum processEnum = ProcessEnum.AGREE; System.out.println(processEnum.status == status);
  3. }

因为枚举和入参中的status都是包装 类型,所以通过==判等肯定是有问题;

format_png 1

推荐阅读:

1、建议收藏 | Redis 使用 10 个小技巧 2、面试必问的一致性Hash在负载均衡中的应用 3、项目中常用的19条MySQL优化 4、单怼多线程,60道面试题,你能答上几个?(附答案) 5、这21 个刁钻的HashMap 面试题,我把阿里面试官吊打了! 6、Java开发必须掌握的 20+ 种 Spring 常用注解 7、老要求用java将word转为PDF,这么搞? 8、Redis分布式锁没用明白,搞出了大故障… 9、程序员面试 10 大潜规则,千万不要踩坑! 10、【收藏了】10分钟读懂进程线程、同步异步、阻塞非阻塞、并发并行

看完有收获?请转发分享给更多有需要的人

关注 java大数据修炼之道

每天学习java技术,你想学的Java知识这里都有!format_png 2

format_png 3微信扫描二维码,关注我的公众号

写留言

  • #

    如果对你有帮助,请点个”在看 format_png 4“,谢谢 format_png 5

发表评论

表情:
评论列表 (有 0 条评论,161人围观)

还没有评论,来说两句吧...

相关阅读