Objects.equals(a,b)与equals的区别

2022-08-31 12:16:52

一:值是null的情况:

  • a.equals(b), a 是null, 抛出NullPointException异常。 a.equals(b), a不是null,
  • b是null, 返回false Objects.equals(a, b)比较时, 若a 和 b 都是null, 则返回 true,
  • 如果a 和 b 其中一个是null, 另一个不是null, 则返回false。注意:不会抛出空指针异常。

null.equals(“abc”) → 抛出 NullPointerException 异常
“abc”.equals(null) → 返回 false
null.equals(null) → 抛出 NullPointerException 异常

Objects.equals(null, “abc”) → 返回 false
Objects.equals(“abc”,null) → 返回 false
Objects.equals(null, null) → 返回 true

二:值是空字符串的情况:

  • a 和 b 如果都是空值字符串:"", 则 a.equals(b), 返回的值是true, 如果a和b其中有一个不是空值字符串,则返回false;

  • 这种情况下 Objects.equals 与情况1 行为一致。

“abc”.equals("") → 返回 false
“”.equals(“abc”) → 返回 false
“”.equals("") → 返回 true

Objects.equals(“abc”, “”) → 返回 false
Objects.equals("",“abc”) → 返回 false
Objects.equals("","") → 返回 true

三:源码分析

  1. 源码
*@since1.7*/publicfinalclassObjects{privateObjects(){thrownewAssertionError("No java.util.Objects instances for you!");}/**
     * Returns {@code true} if the arguments are equal to each other
     * and {@code false} otherwise.
     * Consequently, if both arguments are {@code null}, {@code true}
     * is returned and if exactly one argument is {@code null}, {@code
     * false} is returned.  Otherwise, equality is determined by using
     * the {@link Object#equals equals} method of the first
     * argument.
     *
     * @param a an object
     * @param b an object to be compared with {@code a} for equality
     * @return {@code true} if the arguments are equal to each other
     * and {@code false} otherwise
     * @see Object#equals(Object)
     */publicstaticbooleanequals(Object a, Object b){return(a== b)||(a!= null&& a.equals(b));}
  1. 说明
  • 首先,进行了对象地址的判断,如果是真,则不再继续判断。
  • 如果不相等,后面的表达式的意思是,先判断a不为空,然后根据上面的知识点,就不会再出现空指针。
  • 所以,如果都是null,在第一个判断上就为true了。如果不为空,地址不同,就重要的是判断a.equals(b)。

四:“a==b”和”a.equals(b)”有什么区别?

  • 如果 a 和 b 都是对象,则 a==b 是比较两个对象的引用,只有当 a 和 b 指向的是堆中的同一个对象才会返回 true。
  • 而 a.equals(b) 是进行逻辑比较,当内容相同时,返回true,所以通常需要重写该方法来提供逻辑一致性的比较。
  • 作者:xiaotian_dev
  • 原文链接:https://blog.csdn.net/xiaotian_dev/article/details/113366985
    更新时间:2022-08-31 12:16:52