• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

null != obj vs. obj != null

 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fellas,

Just stumbled back upon this ancient problem during a discussion in meeting.
The arguments just won't move. The problem is simple




Out of the above two, which one is the suggested practice and why? And how does it matter to the JVM, if it does.
Thanks for you time for replying to this.

 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you prefer - to check if 0 is greater than 1 or to check if 1 is less than 0?

I practice obj != null and only because I prefer to compare null against object not object against null. :)
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your quick response to this Zandis.

Well, personally I ususally do a null != object .. lol

The thing is there has to be a standard convention and the discussion won't end, and moreover, does this really affect the performance or any parameters.
Well, I am just looking for a rationale over this.

Thanks Again.
 
Marshal
Posts: 80259
428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no significant difference between if (obj != null) . . . and if (null != obj) . . .

In C it is possible to write if (obj = null) . . . by mistake (when you intended to write == null) and you get two errors for the price of one:
  • The object reference is set to null
  • The condition after the if becomes false implicitly
  • So the practice grew up of writing if (null == obj) . . . because then the compiler will pick up the mistake. That can't happen in Java (and I think, not in C#), because the compiler will pick it up and give an error. There is still a risk of errors in Java if you write == false or == true, however.

    Actually, I think in C the custom is to use a "#define NULL = (0)" statement, then you write NULL rather than null.
     
    Ranch Hand
    Posts: 237
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anubhav Anand wrote:The thing is there has to be a standard convention and the discussion won't end, and moreover, does this really affect the performance or any parameters.
    Well, I am just looking for a rationale over this.



    Both are equivalent. I wouldn't be surprised if a compiler would produce the same bytecode in both cases.

    What's important here is common usage and from this perspective (obj != null) is the overwhelming winner. I'd say 99% of all programmers use it. So if you want to produce code that appeals to the largest number of programmer, use (obj != null).

    There is a situation in Java where the common usage isn't always preferred and that's for this,

    str.equals("literal")

    Instead this is often used,

    "literal".equals(str)

    But there's a good reason for this and this is that the latter avoids a Null Pointer Exception if str is null.

    I think only when there's a very good reason should you go against common usage. And each language has it's own logic for what's preferred. For example someone with a C++ background whould do this,

    for (int i=0, i<10; ++i)

    but a Java programmer would look twice at ++i and think what the f...
     
    Sheriff
    Posts: 28370
    99
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anubhav Anand wrote:The thing is there has to be a standard convention...



    Why does there have to be a standard convention?
     
    Anubhav Anand
    Ranch Hand
    Posts: 341
    Firefox Browser Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the response fellas. I get that. Just that the discussion that had started this 'evil dead' thing would keep on looking for which one is good practice as per standards. But, the doubts are settled now. Thanks a lot for your time and replies.

    Cheers.
     
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anubhav Anand wrote:Thanks for the response fellas. I get that. Just that the discussion that had started this 'evil dead' thing would keep on looking for which one is good practice as per standards. But, the doubts are settled now. Thanks a lot for your time and replies.

    Cheers.




    out of curiosity, what did you settle on ?
    i personally find



    more easier to Read, I believe like many others here, both the cases have no impact on performance.
     
    Anubhav Anand
    Ranch Hand
    Posts: 341
    Firefox Browser Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey Salvin,

    Well, the settlement is that all the people of my group continue to use what they feel better suits them. I am addicted to null != object
    so, maybe I'll continue with it .. or maybe I'll consciously change. Can't say that .. lol

    But, the main issue was if the syntax matters. And since it doesn't we are all on our happy ways.

     
    salvin francis
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hmm
     
    Ranch Hand
    Posts: 483
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well for me..
    (null != obj)

    I use this as my standard. There can be any standard for writing this. Its what a person feels comfortable writing.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic