-1

I have two scenarios given below.

Case1:

if( folderProcessChecklistBean.getFolderProcessChecklistRecord()
                   .getChecklistCode() != null)

Case2:

if(!folderProcessChecklistBean.getFolderProcessChecklistRecord()
                   .getChecklistCode().equals(null))

Can anyone tell me if both approaches are the same or not?

Sitansu
  • 3,225
  • 8
  • 34
  • 61

6 Answers6

6

You can never call the .equals() method on an object reference that is null. It is a method, just like the rest, so if the object reference is truly null it will just throw a NullPointerException.

If you want to check if a reference is not pointing to any object, i.e. it is a null reference you must use the ==.

The equals() method on the other hand is used if you want to compare the data contained in the object. If the class in question overrides the equals(), like for example String does, then it will compare the contents of the object, rather than just whether it is the same object reference.

If you see an object like a box. == is comparing whether it is the same box while equals() compares the contents of two boxes.

jbx
  • 21,365
  • 18
  • 90
  • 144
3

You should always use

  myObject != null

because if myObject is null

  !myObject.equals(null); // <- Exception! null.equals(...); doesn't work!
  myObject != null;       // <- Quite OK 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
2

Case 2 will not work. If the result you want to compare is null, the call of the equals(Object) method will raise a NullPointerException. So you have to use case 1.

Robin Krahl
  • 5,268
  • 19
  • 32
1

The results will be the same if Object returned by getChecklistCode() implements equals according to Object.equals contract which says x.equals(null) should return false

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

== operator is used to compare 2 objects reference equality

equals() check the value of the objects are same

Case 1

if( folderProcessChecklistBean.getFolderProcessChecklistRecord()
                   .getChecklistCode() != null)

it will check the object doesn't have any object reference assigned to it

case 2:

 if(!folderProcessChecklistBean.getFolderProcessChecklistRecord()
                       .getChecklistCode().equals(null))

equals is a method, if you try to invoke it on a null reference, it will Throws a NullPointerException, you can't dereference object, it doesn't refer to anything

Nambi
  • 11,944
  • 3
  • 37
  • 49
  • 2
    This is not the question as asked and even if it was, it would've been a wrong answer. It's the exact opposite. – Jeroen Vannevel Mar 05 '14 at 12:28
  • The error still remains: `==` checks reference equality whereas `.equals()` check value equality. The reason why you should use reference equality is to avoid a NPE, as described in a few others above. – Jeroen Vannevel Mar 05 '14 at 12:37
  • `==` check if two objects are the same, `equals()` compare two objects – Moha the almighty camel Mar 05 '14 at 12:38
  • @JeroenVannevel can you please check my edit answer – Nambi Mar 05 '14 at 12:43
  • Honestly your answer is now just a combination of my words with yours and makes very little sense. I think it would be a good idea if you'd read through [this](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) and [this](http://stackoverflow.com/questions/4501061/java-null-check-why-use-instead-of-equals) answer to understand the nuances. – Jeroen Vannevel Mar 05 '14 at 12:46
  • @JeroenVannevel now it's better – Nambi Mar 05 '14 at 12:59
0

You can get an exception if you use Case2

If folderProcessChecklistBean.getFolderProcessChecklistRecord().getChecklistCode() return null, then it'll throw an exception to use equals.

<Object> obj = folderProcessChecklistBean.getFolderProcessChecklistRecord().getChecklistCode();
if (obj!=null){...}
Andynedine
  • 441
  • 5
  • 20