2

Is there a difference between null != something and something != null in Java. And if there is a difference then which one should I use and why??

Tom
  • 15,798
  • 4
  • 37
  • 48
M.J.
  • 16,266
  • 28
  • 75
  • 97
  • It's called yoda condition. I answered [why it's not-so-useful in Java](http://stackoverflow.com/questions/13887559/is-it-appropiate-the-statement-if-0-expression-or-variable-in-java/13887579#13887579) but somewhat used in C/C++ etc. – P.P Jan 29 '13 at 08:49

7 Answers7

5

There's no difference between null != something and something != null. You must be thinking about the person.getName().equals("john") and the "john".equals(person.getName()) difference: the first one will throw a NullPointerException if getName() returns null, while the second won't. But this is not applicable for the example of your question.

sp00m
  • 47,968
  • 31
  • 142
  • 252
5

its probably comming from the so-called joda-conditions where you write "bla" == myVariable instead of myVariable == "bla" because it could happen to accidentially write myVariable = "bla" which returns "bla" in some languages but also assign "bla" to myVariable

wrm
  • 1,898
  • 13
  • 24
4

I just want to point out that the "Yoda condition" rationale for doing this (in languages like C & C++) does not apply in this (Java) case.

  1. Java does not allow general expressions to be used as statements, so both

     something == null;
    

    and

     null == something;
    

    would be compilation errors.

  2. The types of something == null and something = null are different; boolean and some reference type respectively. In this case, it means that both:

     if (something = null) {
         ...
     }
    

    and

     if (null = something) {
         ...
     }
    

    would be compilation errors.

In fact, I can't think of a realistic example where null == something would be compilation error and something == null would not. Hence, it doesn't achieve anything in terms of mistake-proofing.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • That's also the case in C#. Mistakenly used "=" in a boolean condition instead of "==" results in a compilation error. Yoda condition does nothing here. – Jürgen Bayer Aug 02 '13 at 06:07
1

There is no difference, but some people use it for ease of readability in their code.

Nathan White
  • 1,082
  • 7
  • 21
1

Point of view of performance there will be no difference, both sides of the operator are executed any way. But for a more readable code second one seems more readable

  obj.getSomething().getAnotherThing().doSomething() != null

  null != obj.getSomething().getAnotherThing().doSomething()

But if you are going to just compare a variable or parameter this is more readable

  something != null

Of course this depends on sense of reader.

Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78
0

In java if we compare any, always we have to place variables at left hand side and values are placed at right hand side...

Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
0

They are both the same there is no difference.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130