Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between null==object and object==null [duplicate]

Hi I would like to know diff between the above comparisons?

I am getting null pointer exception when I check object.getItems() == null. But if I change it to null == object.getItems(), it workes fine.

I did look into this what is the difference between null != object and object!=null But I didnt get satisfactory answer.

like image 946
priyank Avatar asked Jun 09 '10 08:06

priyank


People also ask

What is the difference between null object and object == null?

There is absolutely no difference in either semantics or performance. The == in this case is a reference inequality operation; it can never throw NullPointerException .

How do you check if an object is equal 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 .

How do you compare null objects?

To check if it is null, we call the isNull() method and pass the object getUserObject as a parameter. It returns true as the passed object is null.

What does it mean for an object to be null in Java?

In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral ("null") behavior. The null object design pattern describes the uses of such objects and their behavior (or lack thereof).


1 Answers

(Similar question: Which is more effective: if (null == variable) or if (variable == null)?)

Difference between null==object and object==null

There is no semantical difference.

object.getItems() == null and null == object.getItems() are equivalent.

Perhaps you're mixing it up with the fact that

nonNullObj.equals(obj)

and

obj.equals(nonNullObj)

can make a difference (since the second alternative could result in a NPE in case the callee is null).

like image 141
aioobe Avatar answered Oct 22 '22 07:10

aioobe