5

I was watching a java programming video tutorial, and it was mentioning that:

if(null != x){
}

is it a good practice to use it like above ?

Does it differ from :

if(x != null){
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Adham
  • 63,550
  • 98
  • 229
  • 344
  • 3
    No difference, I've found `if (x != null)` to be more common. – ryanpattison Nov 06 '14 at 23:27
  • @rpattiso me too, that why I am asking, a professor from well known university used the first one – Adham Nov 06 '14 at 23:28
  • 4
    Some people prefer to put the `null` first, in case of typo to `null = x`. But the comparaison yields the same results. – AntonH Nov 06 '14 at 23:29
  • @AntonH true but that is a problem in C, not Java right?. – ryanpattison Nov 06 '14 at 23:30
  • Lots of C developers tend to use `null !=`, I tend to use the first. When using `equals` I look for the the mostly not `null` value, for example `"Test String".equals(someStringVariable)`, but that's a different case... – MadProgrammer Nov 06 '14 at 23:30
  • @rpattiso No, you can do assignments within `if` statements, just like C – MadProgrammer Nov 06 '14 at 23:30
  • @rpattiso Nah, can happen in Java also. You're just assigning inside the parentheses. – AntonH Nov 06 '14 at 23:31
  • You may also find it has to do with the way people think "is x not equal to null?" translates to `x != null`... – MadProgrammer Nov 06 '14 at 23:32
  • 2
    In case of `==` `if (null == x)` prevents you from accidentally assigning `null` to `x` in case of typo like `if (x=null)`. But in case of `!=` this mistake can't be done so you are free to chose whichever way you want (but if you start using `null == x` you probably should also use `null != x`). – Pshemo Nov 06 '14 at 23:35
  • 2
    @MadProgrammer True, but most of the time it fails to compile because the assignment expression evaluates to a non boolean type. In C this typo always results in valid code. – ryanpattison Nov 06 '14 at 23:35
  • 1
    @AntonH It can happen, but not likely. `if (x = null)` would be legal *only* if `x` is a `Boolean` (with an upper-case `B`). Otherwise either the expression type would not be boolean, or `null` could not be assigned to `x`. – ajb Nov 06 '14 at 23:37
  • @rpattiso If `x` is an `Object`, which it would need to be in order to be evaluated to `null` then `if (x = null)` is a valid assignment, `if (null = x)` couldn't work...or am I missing the point? – MadProgrammer Nov 06 '14 at 23:40
  • @ajb Wouldn't `x` only need to be a `Object` type? What's the point of trying to evaluate `boolean` to `null`? – MadProgrammer Nov 06 '14 at 23:41
  • @MadProgrammer The result of the assignment expression matches the variable type, `Boolean x; if(x = null);` is valid but `String x; if (x = null);` is not valid Java because the result of `x = null` is a not a boolean type. – ryanpattison Nov 06 '14 at 23:43
  • @rpattiso The assignment is valid, it's the evaluation which is not, `if ((x = null) != null)` will work, but yes, it's a good catch on the side of the compiler ;) – MadProgrammer Nov 06 '14 at 23:45
  • @MadProgrammer If `x` is an `Object`, the type of `x = null` is also an `Object`. And you can't use an `Object` as the condition of an `if` statement. We're still talking about Java, right? – ajb Nov 06 '14 at 23:45
  • @ajb Yes, but the assignment can work, but not the evaluation. It could be possible to have a compound condition where an assignment occurs (by accident), followed by an evaluation, allowing the `if` statement to work - but your point is clearer now ;) – MadProgrammer Nov 06 '14 at 23:47
  • @MadProgrammer I'm not sure what kind of compound condition that would be... Java doesn't allow `x=null && y==null` either. I think the number of cases where you could replace `==` with `=` in an expression, and both would be legal, is quite small. I wish it were zero, though. – ajb Nov 06 '14 at 23:51
  • @ajb `if ((a = b) != null)` ... yes, it's weird, but it's doable ;) - But your previous comment cleared my confusion, cheers ;) – MadProgrammer Nov 06 '14 at 23:52
  • @MadProgrammer I was talking about cases where `=` could be (accidentally) interchanged with `==`, and `if ((a == b) != null)` doesn't seem legal in any case. – ajb Nov 06 '14 at 23:54
  • @ajb True that would need to be `!(a==b)`...hmm. None the less, it's cleared the air ;) – MadProgrammer Nov 06 '14 at 23:55

2 Answers2

5

You're going to want to use the second one.

Both do the same exact thing...compare A to B, or compare B to A - both mean the same thing.

It just makes more sense to use x != null because that is more like how we would say this.

You could ask me "Is x not null?" That seems more natural than "Is null not x?" It makes more sense to us.

Alex K
  • 8,269
  • 9
  • 39
  • 57
  • *You could ask me "Is x not null?" That seems more natural than "Is null not x?"* - that is why `value == variable` is called [yoda condition](http://en.wikipedia.org/wiki/Yoda_conditions) :) – Pshemo Nov 06 '14 at 23:37
  • Haha very cool link. Very interesting – Alex K Nov 06 '14 at 23:48
  • 2
    The trouble with the "yoda condition" is that we would be allowed to use `do` in our Java programs but not `try`..... – ajb Nov 06 '14 at 23:53
  • sorry, maybe you haven't seen the movie... – ajb Nov 06 '14 at 23:56
  • "No. Try not. Do... or do not. There is no try." :) Didn't catch that at first haha nice – Alex K Nov 07 '14 at 00:13
2

I think the most common method used is the latter, but both achieve the same thing.

Harry
  • 3,031
  • 7
  • 42
  • 67