Coding Standard for Zero Validation

Possible duplicates:
What is the difference in comparison? Zero check in Java

Most developers are in the habit of writing a null check with a null value on the left side. For instance,

if(null == someVariable)

Does it help anyway? In my opinion, this affects the readability of the code.

+5
source share
3 answers

No, this has no purpose in Java.

In C and some related languages, it was sometimes used to avoid this error:

if (someVariable = null)

Pay attention to =, not to ==, the author has inadvertently assigned nullto someVariableinstead of checking for null. But this will lead to a compiler error in Java.

C if (someVariable = null) ( ).

, — : " 21, " ( , ). , ; , , .

+13

, " ", C = ==:

// OOps forgot an equals, and got assignment
if (someVariable = null) 
{
}

#/Java/++/C (, , ).

if (someVariable == null) 
{
}

, null.

+5

In your case, I do not see any merit in this. But I prefer the following ...

if("a string".equals(strVariable))
{
}

above this..

if(strVariable != null && strVariable.equals("a string"))
{
}
0
source

All Articles