| Author |
Null comparision
|
Prasath Premkumar
Ranch Hand
Joined: Nov 13, 2008
Posts: 43
|
|
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
Joined: Jan 28, 2008
Posts: 5575
|
|
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
Joined: Jan 26, 2007
Posts: 160
|
|
@ 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
Joined: Jul 26, 2007
Posts: 8430
|
|
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
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Surely you are right. I missed the switch inside the if-statement, and was still reading "if (value = NULL)".
|
 |
 |
|
|
subject: Null comparision
|
|
|