Possible Duplicate:
What is the difference between these (bCondition == NULL) and (NULL==bCondition)?
please see the below code:-
if(myVariable==5)
{
//some logic
}
one of my friends says that this is not a good way to write the code as its not per guidelines, however he dont have any reason to it. Is there is a chance of exception with above code or accidental modification?? According to him the better way would have been
if(5==myVariable)
{
//some logic
}
please let me know which is a better way and why??? Do provide links if u have any.
The only reason to write:
instead of
is that in the former case if you incorrectly put an assignment (single
=) in place you will get a compile time error because you are trying to overwrite a constant.However any decent compiler will give you a warning if you do:
so IMHO it’s not worth worrying about. I always use the latter
if (var == num)form.However in Java there is a common pattern that is worth using. When testing a string for equality, one should use:
instead of:
since the latter can trigger a null pointer exception, and the former cannot.