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
Paul Bilnoski - 11 Jan 2006 18:15 GMT
> Hi all,
>
[quoted text clipped - 16 lines]
>
> 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
Thomas Hawtin - 11 Jan 2006 18:23 GMT
> I have seen the following test for null written
>
[quoted text clipped - 6 lines]
> 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

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Tony Morris - 11 Jan 2006 22:06 GMT
> Hi all,
>
[quoted text clipped - 16 lines]
>
> 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.

Signature
Tony Morris
http://tmorris.net/
Roedy Green - 11 Jan 2006 22:13 GMT
On 11 Jan 2006 09:52:18 -0800, "java_programmer"
<davidebacci@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>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 ==.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
java_programmer - 12 Jan 2006 11:46 GMT
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.
Scott W Gifford - 13 Jan 2006 03:29 GMT
> 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.
Stefan Ram - 13 Jan 2006 03:45 GMT
>> 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«.
Eric Sosman - 13 Jan 2006 17:05 GMT
Stefan Ram wrote On 01/12/06 22:45,:
>>>if(null != myObject) [...]
>>>as opposed to
[quoted text clipped - 6 lines]
> 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 ...

Signature
Eric.Sosman@sun.com
Stefan Ram - 13 Jan 2006 17:27 GMT
>> In C, I'd prefer to write »if( myObject )« instead
>> of »if( myObject == NULL )« or »if( NULL == myObject )«,
[quoted text clipped - 3 lines]
>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.