Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two objects, either of which could be null

Tags:

java

I have a function in one of my classes that compares itself with another instance of the same class - and finds out which variables differ. This is for the purpose of minimizing network load with a main database (by only uploading data that needs to be uploaded, instead of uploading the whole object).

For this, I have been trying to make use of the object.equals() function to compare the two objects.

I soon found that the object.equals() does not handle nulls, and after reading this question, I understand why.

So an example of my broken code is as follows:

public class MyObject {

    String myString;
    String myString2;

    public String getChangedVars(MyObject comparisonObj) {
        ArrayList<String> changedVars = new ArrayList<String>();

        if (!this.myString.equals(comparisonObj.myString))
            changedVars.add("myString");
        if (!this.myString2.equals(comparisonObj.myString2))
            changedVars.add("myString2");

        return changedVars.toString();
    }
}

My question is - on the basis that either one of the variables being compared could be null, what is a simple way to compare two variables whilst avoiding a NullPointerException?

Edit: Simply checking for null on both objects first doesn't work well, as I still want to compare if the object has a reference or not. Eg, if one item is null and the other is not, I want this to resolve to true, as the variable has changed.

like image 834
Mike Baxter Avatar asked Feb 04 '14 09:02

Mike Baxter


People also ask

How do you compare objects to null?

Typically, you'll check for null using the triple equality operator ( === or !== ), also known as the strict equality operator, to be sure that the value in question is definitely not null: object !== null . That code checks that the variable object does not have the value null .

Can we compare 2 null values?

The compare() method in StringUtils class is a null-safe version of the compareTo() method of String class and handles null values by considering a null value less than a non-null value. Two null values are considered equal.

How do you compare two values of objects?

The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address. Syntax: public boolean equals(Object obj)

Can you compare with null Java?

We can simply compare the string with Null using == relational operator. Print true if the above condition is true. Else print false.


2 Answers

There is new utility class available in jdk since 1.7 that is 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.

You can use Objects.equals, it handles null.

Objects.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.

if(Objects.equals(myString,myString2)){...}
like image 60
Subhrajyoti Majumder Avatar answered Oct 27 '22 00:10

Subhrajyoti Majumder


You can use Apache object utils

ObjectUtils.equals(Object object1, Object object2) -- Returns boolean

this method has been replaced by java.util.Objects.equals(Object, Object) in Java 7

like image 20
gowtham Avatar answered Oct 26 '22 23:10

gowtham