Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java null.equals(object o)

Tags:

java

object

null

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 :'''(");
            }
like image 664
Sarah Taylor Avatar asked Jun 13 '12 09:06

Sarah Taylor


People also ask

Can you == null in Java?

7. == and != The comparison and not equal to operators are allowed with null in Java. This can made useful in checking of null with objects in java.

What happens if you compare an object to null using equals ()?

Code Correctness: null Argument to equals()equals(null) will always be false. The program uses the equals() method to compare an object with null . This comparison will always return false, since the object is not null . (If the object is null , the program will throw a NullPointerException ).

Is null is an object in Java?

No , null is not an object.It is a reference type and its value does not refer to any object and so there is no representation of null in memory.

IS null == null in Java?

out. println("(Object)string == number: " + ((Object)string == number)); To conclude this post and answer the titular question Does null equal null in Java? the answer is a simple yes.


4 Answers

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)
like image 114
mikera Avatar answered Oct 19 '22 03:10

mikera


You will need to check atleast one is not null before doing equals method -

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

here s1==s2 will work incase of null==null . But if even one is not null, then you need to check atleast s1 before doing equals.

Update: As edited by @'bernard paulus', if you are using Java 7, you can use java.util.Objects.equals(Object, Object)

like image 29
Kshitij Avatar answered Oct 19 '22 04:10

Kshitij


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.

like image 21
Justinas Jakavonis Avatar answered Oct 19 '22 05:10

Justinas Jakavonis


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

like image 5
Simon Avatar answered Oct 19 '22 05:10

Simon