47

My boss said I should use null == obj, because it's better than obj == null, but he didn't remember why to do this. Is there any reason for using null == obj?
I feel it somehow... opposite!

After some search on Google, the only thing I found is:

in C, it prevents you accidentally from typing (obj = null) in a conditional structure.

Jubin Patel
  • 1,959
  • 19
  • 38
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • 7
    The only thing I was aware of is the thing you found on Google. This is probably your boss being conditioned by C programming. That being said, I wouldn't fight it. – Patrick87 Jul 30 '11 at 14:10
  • 2
    I think that this idea is a hold-over from C. – Hovercraft Full Of Eels Jul 30 '11 at 14:11
  • 2
    Definitely a C thing. I wouldn't go for it personally, since it reads completely backwards. – Ken Wayne VanderLinde Jul 30 '11 at 14:16
  • So... it's useless and non-natural in Java? I don't mean only performance benefits, just any benefits (null == obj) may give? I'm hoping there will be a better answer, so if there's no better answer tomorrow, I will mark Femaref's answer. – Luke Vo Jul 30 '11 at 14:29
  • I'm reading this question in 2020 and I would like to say that "null == obj" tickles my brain every time I see it, and it is also the reason why I came to this page. It seems unnatural, and for that reason, I would say that writing "null == obj" is less productive than "obj == null" because it annoys the majority of future developers/maintainers of your code. Of course, you can get used to something, but if it is unnecessary then you don't have to. – OuuGiii Sep 17 '20 at 12:54

6 Answers6

47

You can't accidently assign null to obj by typing obj = null instead. However, that's a reminiscence from C times, in java, it is not possible, as the = expression returns the right hand side of the assignment. As null is not a boolean, the compiler will complain.

I would try to explain it to my boss once, demonstrate it. If he still doesn't agree with you, just do it. It's a petty thing to fight about with your boss.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • 12
    In general that statement is true, but with one exception: If `obj` is a `Boolean`, then `if (obj = null)` will not trigger a compiler error –  Jul 30 '11 at 14:13
  • On the Google result, he (she) explained: `In C it is possible to write if (obj = null) . . . by mistake (when you intended to write == null) and you get two errors for the price of one: The object reference is set to null The condition after the if becomes false implicitly` – Luke Vo Jul 30 '11 at 14:14
  • @a_horse_with_no_name: Boolean is an object type, it cant be set to null – Daniel Jul 30 '11 at 14:20
  • @Dani: of course you can set a `Boolean` variable to null. Why not? `Boolean flag; flag = null;` and therefor `if (flag = null)` will compile without a problem. –  Jul 30 '11 at 14:25
  • @Dani: Aren't you mixing things up? Primitive types are the ones that can't be null, Boolean objects (the boxed type, not the primitive) can for sure be null. – danielkza Jul 30 '11 at 14:25
  • @a_horse `if(flag=null)` will give an error because `flag=null` is not a boolean expression and thus cannot be used in a conditional – ratchet freak Jul 30 '11 at 14:30
  • 3
    @ratchet freak: try it with flag being defined as `Boolean` :) –  Jul 30 '11 at 14:33
23

If you compile your file with if(null==obj), the generated byte code is if_acmpne and in case of if(obj==null) it is ifnonnull. Now in if_acmpne two operands are popped out of stack and checked they are not equal(in this case null and obj), and in ifnonnull, only one operand is popped and checked if it is not null. From this it seems ifnonnull is better as it involves popping of only one operand.

References : http://www.artima.com/underthehood/flowP.html

Machavity
  • 30,841
  • 27
  • 92
  • 100
Mohammad Suhaib
  • 231
  • 2
  • 4
17

In Java, there is no difference.

I prefer (obj==null) since it feels more natural.

Olle Hallin
  • 588
  • 1
  • 3
  • 12
2

If you do pass a condition as such if (obj=null) or if (null=obj) in present day java IDE's, it will highlight it as syntax error, In addition - attempt to compile will signal the error.

Both (obj==null) and (null==obj) are acceptable, they both carry the same overhead, the later do not yield any performance whatsoever. Decision to using either depends on code style adopted to maintain uniform style across classes.

Kai
  • 38,985
  • 14
  • 88
  • 103
Bitmap
  • 12,402
  • 16
  • 64
  • 91
1

I have never heard of this before, but it seems like the reasoning you gave is solid. I also think it feels backwards but, it should not have any issues other than "feeling" wrong. I don't think it would have any performance benefits or anything like that.

Paul
  • 948
  • 8
  • 17
0

:)

One thing I've found useful about this is that the below syntax (absolutely valid from my POV) is not causing spotbugs' DC-DOUBLECHECK warning, allowing to raise spotbugs threshold without any special exclusion etc:

if (defaultClient == null) {
    synchronized (DEFAULT_CLIENT_INIT_LOCK) {
        if (null == defaultClient) {
            ...
        }
    }
}
GullerYA
  • 1,320
  • 14
  • 27