两个integer 可以用equals比较吗_比较两个Integer,为什么必须使用equals?不用==

超、凢脫俗 2022-10-24 02:04 221阅读 0赞

2b825f74d98abd779900f808e5a337b0.png

作者:码农的一天

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

equals和==的区别

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

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

测试 Integer用例

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

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

为什么结果是true呢?

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

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

  1. 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 = static final int low = -128;

IntegerCache.high = 127

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

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

为什么会返回false呢?

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

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

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

  1. 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[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} }

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

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

结果为false,为什么呢?

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

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

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

结果为false,为什么呢?

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

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

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

结果为true,为什么呢?

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

总结:

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

Integer的误用

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

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

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

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

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

b8bd350379e8e1975eaebe89338b5dcc.gif

  • 面试前突击Spring,我只需要十分钟,那么你呢?
  • springboot2.X手册:redis的7种类型100个方法全解析
  • springboot2.X手册:是时候用Lettuce替换Jedis操作Redis缓存了,倍爽!
  • 为什么Java中1000==1000为false,而100==100为true?
  • SpringBoot:掌握这两个属性,你的测试类可以启动的更快些

#

``

``

#

好文章,我在看

``

发表评论

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

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

相关阅读