aspose file tools*
The moose likes Java in General and the fly likes Equality query! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Equality query!" Watch "Equality query!" New topic
Author

Equality query!

Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
While studying some codes(from many different projects)I have noticed that some programmers prefer to use
if(null != object )
instead of
if(object != null)
Is there is any advantage of using the first as compared to the second.


Rahul Bhattacharjee
LinkedIn - Blog
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
It's a habit you pick up from seeing too many NullPointerExceptions.

Consider the following snippet:



Now consider the following code:



Example one can generate a NullPointerException unless you explicitly check for a null parameter. When querying databases this can be time consuming.

Example 2 will never raise a NullPointerException, since "bar" is never null, and (any non-null string).equals(null) will always return false.

I always use method 2.

Jeremy


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
What you have mentioned is absolutely true, but I have mentioned a different senario.
In my case even if object is null , it would not throw a NPE.
if(object != null)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I guess it's because of symmetry to the well known C(++) idiom to use

if (null == object)

instead of

if (object == null)

The latter can lead to nasty bugs in some languages, because it's so easy to write

if (object = null)

instead.

Of course in Java that last line wouldn't compile at all, so it's an idiom that doesn't apply to Java.

With other words, while that idiom is good to use in some other languages, the only thing it does for Java code is making it harder to read...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
Originally posted by Rahul Bhattacharjee:
What you have mentioned is absolutely true, but I have mentioned a different senario.
In my case even if object is null , it would not throw a NPE.
if(object != null)


Yes, your situation is different, but the idiom is the same. Put an operand of the left hand side of the equality operator that cannot cause some form of RuntimeException or other illegal operation

whether it's "string".equals(arg) or null == arg or result == complicatedVoodooFunction(arg...), it restricts the possible 'error space' and eliminates several classes of simple yet exceedingly vexing bugs. I'd personally rather use the above idiom than spend even 5 minutes bughunting.

Jeremy
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jeremy Botha:

whether it's "string".equals(arg) or null == arg or result == complicatedVoodooFunction(arg...), it restricts the possible 'error space' and eliminates several classes of simple yet exceedingly vexing bugs. I'd personally rather use the above idiom than spend even 5 minutes bughunting.


Only that the "null ==" idiom doesn't reduce the error space in Java at all (although it does in other languages).
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
maybe as of the later compilers. I seem to recall 1.4 compilers happilly allowing you to go if(variable = null) { }

Jeremy
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Thanks to all of you.
So I conclude that it doesnot add any value to java and its matches with what I thought initially about this.
Eitan Levi
Greenhorn

Joined: Jun 14, 2006
Posts: 14
Originally posted by Jeremy Botha:
maybe as of the later compilers. I seem to recall 1.4 compilers happilly allowing you to go if(variable = null) { }

Jeremy


That doesn't sound right ... the value of an assignment expression is of the type you're assigning to, and if statements expect booleans - so you'd probably get a "Type mismatch: cannot convert from <whatever type variable is> to boolean".

Personally though, it freaks me out when I see



I always have to do a double-take and re-read the expression.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equality query!