-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?

2
  • 1
    duplicated so many times Commented Mar 5, 2014 at 12:22
  • 4
    @NimChimpsky This duplicate is wrong. Commented Mar 5, 2014 at 12:25

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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 

Comments

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.

Comments

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

Comments

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){...} 

Comments

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

6 Comments

This is not the question as asked and even if it was, it would've been a wrong answer. It's the exact opposite.
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.
== check if two objects are the same, equals() compare two objects
@JeroenVannevel can you please check my edit answer
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 and this answer to understand the nuances.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.