• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null comparision

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi all,

I heard that when we compare null to a value it is a good practice to do " if(null == value)" rather than
doing " if(value == null)" . But i didnt understand the reason behind it.

Can anyone please give an explanation for doing so???

Thanks.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me both are same almost. I use if(var == null) this way only.

this habhits[if(null == value)] comes from C language . example,in C if statement validate 1 as true and 0 as false
so when you say accidently if(i = 2) insteadof if(i == 2) , you get different output.

but in Java if staement condition must be a boolean , so it is not a problem [except Boolean].

note: only when you are comparing a *Boolean* value you need to be careful because if(Boolean = null) will pass the compiler check.

also this topic discussed here already. Search...

hth
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Seetharaman Venkatasamy good explanation

Actually I prefer 'value==null' as it seems more natural to me as I am checking 'value' to be null and not the other way round
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the case of value==null it does not matter much.
However if you are using aValue.equals(bValue) it is always better to use the pattern knownValue.equals(unknownValue) to avoid the NPE
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are reasons for preferring if (null == value) in C/C++ because you will get a compiler error if you write = by mistake instead of ==. That is because C cannot distinguish 0 for false and 0 for null and will accept (value = null) as the same as false. Only in C you usually say NULL, rather than null. But you can't writeif (NULL = value) even in C.

In Java, however, the compiler will never allow if (null), so you will always get a compiler error if you write if (value = null) instead of == null. In that case there is no need to prefer if (null == value) and I would agree with Maneesh and Manish.

You should, however, always avoid == false and == true, because it is possible to change values with = unintentionally.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There are reasons for preferring if (null == value) in C/C++ because you will get not a compiler error if you write = by mistake instead of ==.


There, I fixed it. C and C++ allow it.

There is in Java one reference type that does allow this to compile since Java 5.0: Boolean. Consider the following example:
In Java 1.4 and before this would give a compiler error. Because of auto-boxing this will now compile but lead to a NullPointerException. The reason is because this is what happens:
- null is assigned to b
- b is auto-unboxed from Boolean to boolean, but auto-unboxing null references throws an NPE.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely, Rob, C will give a compiler error if you write if (NULL = value) and not if you write if (value = NULL)?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely you are right. I missed the switch inside the if-statement, and was still reading "if (value = NULL)".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic