Testing for null - simple!

Discussion in 'Java' started by java_programmer, Jan 11, 2006.

  1. 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
    java_programmer, Jan 11, 2006
    #1
    1. Advertising

  2. java_programmer wrote:
    > 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
    Paul Bilnoski, Jan 11, 2006
    #2
    1. Advertising

  3. java_programmer wrote:
    >
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Jan 11, 2006
    #3
  4. java_programmer

    Tony Morris Guest

    "java_programmer" <> wrote in message
    news:...
    > 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.

    --
    Tony Morris
    http://tmorris.net/
    Tony Morris, Jan 11, 2006
    #4
  5. java_programmer

    Roedy Green Guest

    On 11 Jan 2006 09:52:18 -0800, "java_programmer"
    <> 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 ==.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 11, 2006
    #5
  6. 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.
    java_programmer, Jan 12, 2006
    #6
  7. "java_programmer" <> writes:

    > 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.
    Scott W Gifford, Jan 13, 2006
    #7
  8. java_programmer

    Stefan Ram Guest

    Scott W Gifford <> writes:
    >> 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«.
    Stefan Ram, Jan 13, 2006
    #8
  9. java_programmer

    Eric Sosman Guest

    [OT] Re: Testing for null - simple!

    Stefan Ram wrote On 01/12/06 22:45,:
    > Scott W Gifford <> writes:
    >
    >>>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 ...

    --
    Eric Sosman, Jan 13, 2006
    #9
  10. java_programmer

    Stefan Ram Guest

    Re: [OT] Re: Testing for null - simple!

    Eric Sosman <> writes:
    >> 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 ...


    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.
    Stefan Ram, Jan 13, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,620
    Mike Schilling
    Mar 29, 2006
  2. Brian van den Broek
    Replies:
    3
    Views:
    839
    Jeremy Bowers
    Apr 3, 2005
  3. Brian van den Broek
    Replies:
    2
    Views:
    392
    Brian van den Broek
    Apr 5, 2005
  4. Chris

    Testing and Load Testing

    Chris, Mar 4, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    323
  5. John Maclean
    Replies:
    1
    Views:
    340
    Martin P. Hellwig
    Apr 13, 2010
Loading...

Share This Page