Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null comparision

 
Prasath Premkumar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • 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
 
Maneesh Godbole
Saloon Keeper
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you are right. I missed the switch inside the if-statement, and was still reading "if (value = NULL)".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic