Java null.equals(object o)

2020-03-01 03:07发布

I know it's not possible to call the equals method on a null object like that:

//NOT WORKING
            String s1 = null;
            String s2 = null;
            if(s1.equals(s2))
            {
                System.out.println("NOT WORKING :'(");
            }

But in my case I want to compare two objects from two database and these two objects can have attributes null...

So what is the method to compare two attributes, knowing that we are not sure that the value is null or filled.

This method is good or not ?

//WORKING
            String s1 = "test";
            String s2 = "test";
            if(s1 == s2 || s1.equals(s2))
            {
                System.out.println("WORKING :)");
            }

            //WORKING
            String s1 = null;
            String s2 = null;
            if(s1 == s2 || s1.equals(s2))
            {
                System.out.println("WORKING :)");
            }

I'm not sure because in this case it's not working ... :

//NOT WORKING
            String s1 = null;
            String s2 = null;
            if(s1.equals(s2)|| s1 == s2  )
            {
                System.out.println("NOT WORKING :'''(");
            }

标签: java object null
9条回答
爷的心禁止访问
2楼-- · 2020-03-01 03:46

Invoking a dot operator (call method or object properties) on null object will always throw RuntimeException (Exception are the static members as they don't belong to the object but to the class).

The chunk of code works only because || is a shortcut operator:

 String s1 = null;
 String s2 = null;
 if(s1 == s2 || s1.equals(s2))

it does not evaluate the right side if the left side is true.

Same for the &&: it does not evaluate the right side if the left side is false.

查看更多
不美不萌又怎样
3楼-- · 2020-03-01 03:49

Another option to use:

Objects.equals(identification, criteria.getIdentification())

java.util.Objects

This class consists of static utility methods for operating on objects. These utilities include null-safe or null-tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects.

Since: 1.7

public static boolean equals(Object a, Object b)

Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.

查看更多
来,给爷笑一个
4楼-- · 2020-03-01 03:57

The reason you're getting a NullPointerException when doing s1.equals(s2) with s1 being null is not because of s2 being null, but because you are trying to invoke the equals method on s1, which is null.

Try to amend it like this:

if(s1 != null && s1.equals(s2){ /*...*/ }

Also note that if s1 is not null and s2 is, you'll get a false back from equals, but no NullPointerException.

查看更多
狗以群分
5楼-- · 2020-03-01 04:00

I generally use a static utility function that I wrote called equalsWithNulls to solve this issue:

class MyUtils {
  public static final boolean equalsWithNulls(Object a, Object b) {
    if (a==b) return true;
    if ((a==null)||(b==null)) return false;
    return a.equals(b);
  }
}

Usage:

if (MyUtils.equalsWithNulls(s1,s2)) {
  // do stuff
}

Advantages of this approach:

  • Wraps up the complexity of the full equality test in a single function call. I think this is much better than embedding a bunch of complex boolean tests in your code each time you do this. It's much less likely to lead to errors as a result.
  • Makes your code more descriptive and hence easier to read.
  • By explicitly mentioning the nulls in the method name, you convey to the reader that they should remember that one or both of the arguments might be null.
  • Does the (a==b) test first (an optimisation which avoids the need to call a.equals(b) in the fairly common case that a and b are non-null but refer to exactly the same object)
查看更多
该账号已被封号
6楼-- · 2020-03-01 04:02
        String s1 = null;
        String s2 = null;

        if(s1 != null && s2 != null && s1.equals(s2)) {
            System.out.println("equals");
        } else {
            System.out.println("not equals");
        }
查看更多
Juvenile、少年°
7楼-- · 2020-03-01 04:04

Try using the ObjectUtils class from org.apache.commons.lang

public static boolean equals(java.lang.Object object1,
                         java.lang.Object object2)

From the api docs...

Compares two objects for equality, where either one or both objects may be null.

ObjectUtils.equals(null, null) = true

ObjectUtils.equals(null, "") = false

ObjectUtils.equals("", null) = false

ObjectUtils.equals("", "") = true

ObjectUtils.equals(Boolean.TRUE, null) = false

ObjectUtils.equals(Boolean.TRUE, "true") = false

ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE) = true

ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false

查看更多
登录 后发表回答