Testing for null - simple!

J

java_programmer

Hi all,

I have seen the following test for null written

if(null != myObject)
{
....
}

as opposed to

if(myObject != null)
{
...
}

I have been told that the first version is slightly optimised and more
performant but I can't work out why. Does anyone have any thoughts?

Thanks
 
P

Paul Bilnoski

java_programmer said:
Hi all,

I have seen the following test for null written

if(null != myObject)
{
...
}

as opposed to

if(myObject != null)
{
..
}

I have been told that the first version is slightly optimised and more
performant but I can't work out why. Does anyone have any thoughts?

Thanks

It really shouldn't be any more performant. I suspect it's a carryover
from C/C++ to protect against assigning on accident:

if (myObject = null)
{
... will not execute
}

--Paul
 
T

Thomas Hawtin

java_programmer said:
I have seen the following test for null written

if(null != myObject)
[...]
as opposed to

if(myObject != null)
[...]
I have been told that the first version is slightly optimised and more
performant but I can't work out why. Does anyone have any thoughts?

It really shouldn't make any difference to performance (but check for
yourself if you like). The reason why a minority of programmers prefer
the former is that in C and C++ (but not Java) if you accidentally write
"if (ptr = null)" instead of "if (ptr == null)" (or "if (!ptr)"), then
the code will compile but produce incorrect results. In Java the mistake
can only happens if comparing two booleans, or from 1.5 comparing a
java.lang.Boolean.

Tom Hawtin
 
T

Tony Morris

java_programmer said:
Hi all,

I have seen the following test for null written

if(null != myObject)
{
...
}

as opposed to

if(myObject != null)
{
..
}

I have been told that the first version is slightly optimised and more
performant but I can't work out why. Does anyone have any thoughts?

Thanks

Broad claims regarding performance are almost always false.
This case is no different except that it exhibits the phenomena to an
extreme - that is, even a drunken monkey would call 'bulls**t' on the claim.
I suggest that you question the authenticity of your source, not the source
itself.
 
R

Roedy Green

if(null != myObject)

There is a related pattern:
if ( "blue".equals(skyColour) )...

rather than
if ( skyColour.equals('blue") )...

The first awkward-sounding pattern will not raise an exception if
skyColour is null; it will simply evaluate as false.

In English, "The skycolour is blue" and "the skycolour equals blue"
have different meanings. What you usually mean in Java is closer to
the English "is" even though you express it with equals or ==.
 
J

java_programmer

Thanks all. It seems so obvious now that it has been pointed out and I
really should have know better! I quite like Roedy Greens string
comparison code that means you don't need to check a string for null -
I will look out for that in future,

Much appreciated.
 
S

Scott W Gifford

java_programmer said:
if(null != myObject) [...]
as opposed to
if(myObject != null) [...]

I have been told that the first version is slightly optimised and more
performant but I can't work out why. Does anyone have any thoughts?

Sounds dubious. In C, a common reason to do comparisons with the
constant first is so if you mean "if (myObject == null)" but instead
type "if (myObject = null)", the compiler can catch it (since "if
(null = myObject)" is illegal). Java will catch this error (unless
myObject is a boolean), but I bet this style is mostly a leftover
habit from C. It certainly doesn't hurt anything.

---ScottG.
 
S

Stefan Ram

Scott W Gifford said:
if(null != myObject) [...]
as opposed to
if(myObject != null) [...]
Sounds dubious. In C, a common reason to do comparisons with
the constant first is so if you mean "if (myObject == null)"

In C, I'd prefer to write »if( myObject )« instead
of »if( myObject == NULL )« or »if( NULL == myObject )«,
given that NULL is as defined in »stddef.h«.
 
E

Eric Sosman

Stefan Ram wrote On 01/12/06 22:45,:
Scott W Gifford said:
if(null != myObject) [...]
as opposed to
if(myObject != null) [...]

Sounds dubious. In C, a common reason to do comparisons with
the constant first is so if you mean "if (myObject == null)"


In C, I'd prefer to write »if( myObject )« instead
of »if( myObject == NULL )« or »if( NULL == myObject )«,
given that NULL is as defined in »stddef.h«.

Note that your preferred style does not mean the
same thing as the alternatives. If your preferred
style leads even you to make elementary mistakes, it
may be a good idea to re-examine your preferences ...
 
S

Stefan Ram

Eric Sosman said:
Note that your preferred style does not mean the
same thing as the alternatives. If your preferred
style leads even you to make elementary mistakes, it
may be a good idea to re-examine your preferences ...

Thank you for the correction!

Yes, I should have used "!=" instead of "==".

However, it is only an interpretation that this
mistake of mine was /caused/ by my preferred style.

To me, to test whether an object refered to by the name "o"
exists, it is most natural to write

if( o ){ /* ... */ }

Yes, I learned C first, then Java.

When I am forced to use one of "==" or "!=", it makes me to
start thinking about it and possibly erring -- which also
could be interpret as a cause of my mistake.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top