13

Consider the following two lines of code

if (test ! = null)

and

if (null != test)

Is there is any difference in above two statements, performance wise? I have seen many people using the later and when questioned they say its a best practice with no solid reason.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Sandeep Nair
  • 3,630
  • 3
  • 26
  • 38
  • 3
    This is a duplicate of http://stackoverflow.com/questions/2369226/null-check-in-java, except that this one is using != instead of equals. – Raul Rene May 08 '12 at 07:41
  • Similar question http://stackoverflow.com/questions/15518958/null-check-coding-standard/15519045 – Shreyos Adikari Nov 14 '14 at 21:59
  • While we're on the question of best practices, you may want to start adding spaces between the "if" and "(". Coupling this minor code style change to adhere to java coding conventions will make even the pickiest code reviewer (me) have nothing to say about your if statements. http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html#449 – Russ Dec 14 '15 at 13:50
  • 1
    Does this answer your question? [object==null or null==object?](https://stackoverflow.com/questions/2369226/object-null-or-null-object) – Ivan Dec 05 '20 at 00:26

8 Answers8

22

No difference.

Second one is merely because C/C++ where programmers always did assignment instead of comparing.

E.g.

// no compiler complaint at all for C/C++
// while in Java, this is illegal.
if(a = 2) {
}
// this is illegal in C/C++
// and thus become best practice, from C/C++ which is not applicable to Java at all.
if(2 = a) {
}

While java compiler will generate compilation error.

So I personally prefer first one because of readability, people tend to read from left to right, which read as if test is not equal to null instead of null is not equal to test.

Pau Kiat Wee
  • 9,485
  • 42
  • 40
22

They are exactly the same. The second one can make sense when using equals:

if("bla".equals(test))

can never throw a NullPointerException whereas:

if(test.equals("bla")) 

can.

Pavel
  • 4,912
  • 7
  • 49
  • 69
assylias
  • 321,522
  • 82
  • 660
  • 783
3

There is no performance difference but I personally find the second one being confusing.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
2

Second one looks for me like "Yoda condition", i.e. if(2 == x) ... and is much less readable.

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
1

It's best practice to avoid some basic typo's that most modern IDE's will pick up, because sometimes you want to do comparisons between more complex types that are not null and end up doing accidental assignments. So the pattern remains the same, but I've never seen this linked to performance and have never seen it generate special byte code.

The idea is to have the static, known value first, so you can't throw any kind of weird exception when you perform the comparison.

Neither of the two methods are "more correct" though, so it's entirely up to you to decide what you wish to use.

Ewald
  • 5,691
  • 2
  • 27
  • 31
1

there is no difference. But in second way you avoid the typo like test = null. Beacause in second way you'll get compiler error.

Alex Stybaev
  • 4,623
  • 3
  • 30
  • 44
  • Unless 'test' is not an object of type java.lang.Boolean, you will get a compiler error in the first case, too. Object test = null; if(test = null) { // does not compile. Can not convert Oject to boolean } – Polygnome May 08 '12 at 07:55
1

It's not a best practice to use the latter one. Both are equivalent, but the former is easier to read.

This "best practice" comes from C where boolean don't exist. Integers are used instead, and if (foo = 1) is not a syntax error, but is completely different from if (foo == 1).

In Java, only boolean expressions can be inside an if, and this practice doesn't make much sense.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

There is no really different between two form. There is no performance issue but there are following notes:

  • First form is readable for code reader, because people usually read codes Left-To-Right.
  • Second form is better for code writer, because in java = operator is for assignment and == operator is for test equivalent, but people usually using in if statement = instead of ==, by second approch developer getting Compile-Time-Error because null can't use in Left-Side of a assignment statement.
Sam
  • 6,770
  • 7
  • 50
  • 91