7

What is the difference between null!=variable and variable!=null Which method is perfect to use?

if ((null != value1) || (null != value2) || (null != value3)) {
            .......
            .......
            .......
}

or

if ((value1 != null) || (value2 != null) || (value3 != null)) {
                .......
                .......
                .......
}

Please suggest the best one and the logical change between these?

3
  • 4
    No difference. In the olden days, well perhaps still today ;-), constant != x was often recommended in languages like C. This was called Yoda Conditions and helped to avoid cases like x = constant as constant = x would raise a compiler error. (However, in Java the only case when this matters is when doing a comparison with a boolean which should be avoided anyway.) Commented Sep 17, 2014 at 5:03
  • I have seen codes comparing null with variable, that's why I asked this question. Commented Sep 17, 2014 at 5:06
  • Well, you have to make sure the expression is not null before a method is attempted to be invoked upon it - lest a NPE is raised. However it doesn't matter if it is null != x or x != null. Pick one and be consistent (which also means following the form already used in a project). Commented Sep 17, 2014 at 5:06

6 Answers 6

6

No difference.

But people quite often write "abc".equals(foo), instead of foo.equals("abc"), to avoid null point exception.

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

Comments

4

Nothing is different.

I prefer value != null for purely readability reasons.

Comments

3

Both the ways of comparision are correct.There is logically no difference but programmers prefer to use value!=null.

Comments

2

null != value is a hold-over from C. In C, your code will be compiled with no warning in this way: if (variable = null) (it's a wrong code), while you actually probably meant if (variable == null).

But in Jave both these two styles are OK.

Comments

2

In C assignment looks similar to comparison:

lvalue = rvalue;
lvalue == rvalue;

lvalues (variables) can be assigned to. rvalues can be expressions. Null cannot be assigned to, so if you adhere to use null as the lvalue in comparison, then the compiler will catch the ommision of the second equal sign in a statement like

null = rvalue;

Where you might accidentally assign null with:

lvalue = null;

Comments

1

As others have mentioned there is not a difference but I haven't ran across something that is

null != value2 

I always see it as value2 !=null and like thihara said its easier for readability. I think its also good to keep it the way value != null for beginner programmers who could possibly go over some ones code might get a little lost on the concept even though there is no difference.

Comments

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.