在项目中涉及到整数判断的时候一直都是使用"=="进行判断的,但是用的时候心里老是在犯嘀咕,是不是应该使用equals呀?今天来看下这个问题!

在Object类中,equals方法的定义是这样的,

public boolean equals(Object obj) {
return (this == obj);
}

可见在Object类中中,equals()和"=="是等价的,但是在大部分的包装类中,都重写了equals()方法,所以两者还是有区别的。总的来说"=="是一个关系运算符,如果比较的两端都为原生数据类型,则表示判断两者的值是否相等,如果比较的两端都为引用类型,则比较两者所指向对象的地址是否相同;对于equals()方法,如果这个对象所在的类重写了equals方法,则按照重写的方法进行比较,如果没有,则比较两者所指向对象的地址是否相同,其实就是使用"=="进行比较。

下面就对上面的论述进行测试。

 public class Test {
public static void main(String[] args) {
Integer a = new Integer(200);
Integer b = new Integer(200);
Integer c = 200;
Integer e = 200;
int d = 200; System.out.println("a == b -> " + (a == b));
System.out.println("a.equals(b) -> " + a.equals(b));
System.out.println("a == c -> " + (a == c));
System.out.println("a.equals(c) -> " + (a.equals(c)));
System.out.println("c == e -> " + (c == e));
System.out.println("c.equals(e) -> " + (c.equals(e)));
System.out.println("d == a -> " + (d == a));
System.out.println("a.equals(d) -> " + (a.equals(d)));
System.out.println("d == c -> " + (d == c));
System.out.println("c.equals(d) -> " + (c.equals(d)));
}
}

运行结果

a == b -> false
a.equals(b) -> true
a == c -> false
a.equals(c) -> true
c == e -> false
c.equals(e) -> true
d == a -> true
a.equals(d) -> true
d == c -> true
c.equals(d) -> true

a == b -> false:对于两个new出来的Integer对象,因为每次使用new关键字都会在堆内存中开辟一块空间,存放相应的对象的值,然后在栈内存中存放这块内存地址的引用。而==运算符比较两者所指向对象的地址是否相同,申请了两块空间,地址肯定不相同,所以结果为false。
a.equals(b) -> true:结果为true,查看Integer源码我们发现重写的equals方法是这样的:

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

首先判断传进来的Object类型是不是Integer类的一个实例,如果不是直接返回false;如果是则判断两者的成员变量value值是不是相等的(Integer类中定义的private final int value),这块又回到了基本类型的比较("==")。value的值在创建这个对象的时候被赋值,两个Integer对象传递的参数都为200,所以value值相等,返回true。

代码的第5行Integer c = 200用int给Integer赋值,因为从jdk5开始java引入了自动装箱、自动拆箱机制。第5行就是一个自动装箱的过程,相当于:Integer c = Integer.valueOf(200);在Integer类中,valueOf方法是这么实现的:

/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
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之间,如果参数中的i在这个范围内,则返回一个数组中的内容,如果不在这个范围,则new一个新的Integer对象并返回。查看Integer类的源码可以发现,这个数组里面缓存了基本类型-128-127之间的Integer对象。但是由于第11行是与一个new出来的对象做比较,所以==肯定返回的false。

第12行,equals方法比较两个对象的value值,所以为true。
第13行,两个自动装箱的变量,但是装箱传递的值大于127,都new了一个Integer对象,所以返回false。如果装箱传递的值在-128到127之间,结果为true。
第14行,结果为true。两个自动装箱的Integer对象,比较value。
第15行,这块进行比较的时候,会对Integer对象进行自动拆箱,也就是调用intValue方法。两个基本数据类型进行==判断,根据值比较,所以结果为true。这块可能有人会问,为什么不是对int类型进行自动装箱处理呢?其实这块是java根据一个很明显的道理进行设计的:如果有人比较一个int类型的值和Integer类型的值,是想比较什么呢?肯定是值呀,所以这块是对Integer对象进行拆箱而不是对int类型装箱了。
第16行这块,首先调用equals方法的肯定是Integer对象,但是Integer类中重写的equals方法参数是一个Object类型呀,怎么能传递一个基本数据类型进去呢?所以,这块又有一个自动装箱的操作,当传递一个int类型给equals这个方法时,java会自动将这个值装箱为Integer类,而Integer类的最终父类又是Object,所以这块参数的问题就解决了,然后就是两个Integer对象进行equals判断,返回true。
第17行,首先d为一个基本类型int,c为一个Integer对象,所以进行==比较的时候,肯定会对Integer对象进行拆箱处理,所以结果为true。
第18行,同第16行。

Integer判断相等,到底该用==还是equals的更多相关文章

  1. 基于JavaScript判断浏览器到底是关闭还是刷新(超准确)

    这篇文章主要介绍了基于JavaScript判断浏览器到底是关闭还是刷新(超准确)的相关资料,需要的朋友可以参考下 本文是小编总结的一些核心内容,个人感觉对大家有所帮助,具体内容请看下文: 页面加载时只 ...

  2. [Java]判断Integer值相等最好不用==最好使用equals

    测试代码 Integer c = ; Integer d = ; Integer e = ; Integer f = ; System.out.println(c == d); System.out. ...

  3. 判断Integer值相等最好不用==最好使用equals

    Integer c = 3;Integer d = 3;Integer e = 321;Integer f = 321;System.out.println(c == d);System.out.pr ...

  4. Integer判断大于 == 127时的坑

    在一次判断返回Interger类型的code,  用==结果, 没进去 Integer的值在-128到127时,Integer对象是在IntegerCache.cache产生,会复用已有对象,也就是说 ...

  5. Mybatis if test 中int integer判断非空的坑

    Mybatis 中,alarmType 是int类型.如果alarmType 为0的话,条件判断返回结果为false,其它值的话,返回true. 1 <if test="alarmTy ...

  6. Java基础(六)判断两个对象相等:equals、hashcode、toString方法

    1.equal方法 Object类中的equal方法用于检测一个对象是否等于另外一个对象.在Object类中,这个方法将判断两个对象是否具有相同的引用.如果两个对象具有相同的引用,它们一定是相等的.然 ...

  7. jquery判断滚动条到底

    $(document).scroll(function(){ var dHeight = $(document).height(); var wHeight = $(window).height(); ...

  8. Integer ==判断遇到的问题

      今天开发过程中,遇到 这样的一个问题 public class Test { public static void main(String[] args) { Integer aa = 12345 ...

  9. java 判断 string 转 integer 判断

    NumberUtils.isDigits("1") NumberUtils.isDigits("/") 根据返回 true false 再确定是否转换即可 需要 ...

随机推荐

  1. GUC-5 CountDownLatch闭锁

    /* * CountDownLatch :闭锁,在完成某些运算是,只有其他所有线程的运算全部完成,当前运算才继续执行 */ public class TestCountDownLatch { publ ...

  2. [实战]MVC5+EF6+MySql企业网盘实战(23)——文档列表

    写在前面 上篇文章实现了图片列表,这篇文章实现文档列表将轻车熟路,因为逻辑基本相似,只是查询条件的不同.这里将txt,doc,docx,ppt,pptx,xls,xlsx的文件都归为文档列表中. 系列 ...

  3. ASP.NET中登录功能的简单逻辑设计

     ASP.NET中登录功能的简单逻辑设计                               概述                               逻辑设计             ...

  4. bzoj 1854 并查集 + 贪心

    思路:这个题的并查集用的好NB啊, 我们把伤害看成图上的点,武器作为边,对于一个联通块来说, 如果是一棵大小为k的树,那么这个联通块里面有k - 1个伤害能被取到,如果图上有环那么k个值都能 取到,对 ...

  5. MNIST神经网络的训练

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data 1.设置输入和输出节点的个数,配置 ...

  6. Building Robust and Flexible Event System in Unity3D

    Building Robust and Flexible Event System in Unity3D 1. Prerequisites 1.1 Observer Pattern According ...

  7. springMVC整合freemarker遇到的问题 maven

    java.lang.IllegalAccessError: tried to access method freemarker.ext.servlet.AllHttpScopesHashModel.& ...

  8. 【最短路径】 SPFA算法优化

    首先先明确一个问题,SPFA是什么?(不会看什么看,一边学去,传送门),SPFA是bellman-ford的队列优化版本,只有在国内才流行SPFA这个名字,大多数人就只知道SPFA就是一个顶尖的高效算 ...

  9. aiohttp

    发起请求 async def fetch(): async with aiohttp.ClientSession() as session: async with session.get('https ...

  10. android 启动流程

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha adb shell 后  用 ps 命令 回车 可以看到 运行的进程. 如下结果: ct ...