aspose file tools*
The moose likes Beginning Java and the fly likes null != obj vs. obj != null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null != obj vs. obj != null" Watch "null != obj vs. obj != null" New topic
Author

null != obj vs. obj != null

Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

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.

Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

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

Joined: May 18, 2007
Posts: 341

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39814
    
  28
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.
    Embla Tingeling
    Ranch Hand

    Joined: Oct 22, 2009
    Posts: 237
    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...
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18887
        
        8

    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

    Joined: May 18, 2007
    Posts: 341

    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.
    salvin francis
    Ranch Hand

    Joined: Jan 12, 2009
    Posts: 928

    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.


    My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
    Anubhav Anand
    Ranch Hand

    Joined: May 18, 2007
    Posts: 341

    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
    Ranch Hand

    Joined: Jan 12, 2009
    Posts: 928

    hmm
    Somnath Mallick
    Ranch Hand

    Joined: Mar 04, 2009
    Posts: 477
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: null != obj vs. obj != null