0

I have been going through some site where they talk about how to prevent NPE. There I saw people say use null != object but I don't understand why. What is the difference between doing the below in java?

if(null != object) 

vs

if(object != null)
1
  • 2
    Are you sure they weren't talking about null == object instead? Provide your source so we see the context. Commented Jul 25, 2014 at 23:29

2 Answers 2

4

There is no difference between the two in the effect.

However there is a school of thinking where it is recommended to use un-assignable (constant) values on the left hand side of a operator. Because this reduces the risk of unintended assignment (this is from the time where C compilers have been not warning about it).

// this is dangerous when misstyped
if (object = null) {

The argument that writing the null first really reduces the effect of misstyping is however pretty weak. Especially when not using "==" but "!=" or "<". So I would say, ignore those recommendations.

There are however some situations where order is helpfull to prevent NPE:

if ("string".equals(object))

In this case you dont get an NPE when "object" is null.

Sign up to request clarification or add additional context in comments.

5 Comments

Note that in Java if (object = null) { is a compilation error because the value of object = null is not boolean as requires by the context. The only case where that convention helps in Java is when the variable has type boolean.
It should also be noted that most IDE's nowadays will either give you a warning or fail to compile if(object = null) (I know Visual Studio refuses to). As a sidenote: passing around null values is a sign of a larger architecture problem so I'm not a fan of switching that string around.
@JeroenVannevel - This is a Java question ... check the tags.
@StephenC: I'm aware, it was background information since I wasn't sure about the Java IDE's I've used.
@JeroenVannevel - You shouldn't need to be. Since this is Java, the 'object = null' mistake is a compilation error, irrespective of the IDE or the compiler. The Java language spec says so. Any compiler that doesn't call that a compilation error is 1) non-compliant and 2) going to generate code that will be rejected by the verifier at class load time.
0

What is the difference between doing the below in java?

  • There is no difference in terms of what the code means.

  • There is no difference in terms of code safety / robustness.

In some languages, the if (null == object) pattern helps to avoid this mistake:

 if (object = null)

... which accidentally trashes the value of the object variable.

But in Java, an if statement requires a condition expression whose type is boolean, and the type of object == null is NOT boolean. Hence that particular mistake / typo is guaranteed to give a compilation error.


Digression ...

The case where you plausibly could get into trouble with '==' versus '=' in classic Java is this:

 if (flag == true)

or

 if (flag == false)

However, no experienced Java programmer would write that. Instead they would write:

 if (flag)

and

 if (!flag)

With Java 5 and onwards, autoboxing of Boolean can get you into trouble. However, in that case you are doing something that is inadvisable anyway. Consider this:

 Boolean TRUE = true;
 Boolean TOO_TRUE = new Boolean(true);  // generates a new instance ...

 if (TRUE == TOO_TRUE) {  // should be TRUE.equals(TRUE)
     ....
 }

Again, an experienced programmer would avoid using boxed types unnecessarily, and avoid testing them with ==. Besides, you would probably get a warning for this from an IDE or from a static anaylser like FindBugs or PMD.

1 Comment

It can happen for Boolean thanks to autoboxing. Boolean b = Boolean.TRUE; boolean condition = (b = null); or if (b = null) compiles (with warnings).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.