-1

Possible Duplicate:
Checking for null - what order?

Can anyone please tell me what is the better approach in validating a string value for 'null' and why?

String user = userService.getUserName(accID);

1) if(user != null) {
    ..
   }

2) if(null != user) {
    ..
   }
Community
  • 1
  • 1
Sasha
  • 245
  • 2
  • 4
  • 13
  • If I correctly understood you problem please have a look here: http://stackoverflow.com/questions/2601978/how-to-check-if-my-string-is-equal-to-null – Grijesh Chauhan Oct 11 '12 at 11:18

7 Answers7

2

There is no differnece. The first version is more commonly used.

Grim
  • 1,938
  • 10
  • 56
  • 123
2

For this case it doesn't matter, alltough the first aproach is more common.

However but when feeding it directly into an equals method, the so-called yoda-condition is prefered.

eg:

if("".equals(string)){
}

is prefered over

if(string.equals("")){
}

because it avoids nullpointer exceptions

G-Man
  • 1,321
  • 8
  • 15
  • 1
    Yoda condition with == also avoids *something*---accidental assignment instead of comparison. Since this is a compiler warning by default, though, there's not much sense in guarding against it with unnatural syntax. – Marko Topolnik Oct 11 '12 at 11:39
  • agreed, however within an if statement it only realy can happen with Boolean classes anyway. All other classes will result in a "this will not result in a boolean" kind of error. – G-Man Oct 11 '12 at 11:43
  • True... most often this will occur not when comparing against a literal (which wouldn't make sense for a boolean anyway), but when comparing two boolean values. In that particular case, however, code could really get obfuscated if it weren't for the compiler warning. – Marko Topolnik Oct 11 '12 at 11:47
0

First approach is mostly used by developers. I don't think it makes difference.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

I have seen both approaches. Some people believe it is better to give the value you are comparing to first. This is maybe better if you have many alternatives to see the value you are comparing to first.

I personally use the first option because this is my habit.

I doubt there is any performance difference if that is your question. All the difference is in the code style and readability.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

Since there is no functional difference to it and the first is more readable, you should choose the first one.

exic
  • 2,220
  • 1
  • 22
  • 29
0

It is a matter of taste in this case.

In other cases, e.g. if you would compare against a string literal/constant, I would prefer this:

if ("yourStringConstant".equals(yourVariable))

as this would also handle the case when yourVariable would be null. If you would do it the other way, you would get a NullPointerException:

if (yourVariable.equals("yourStringConstant"))
erdal.karaca
  • 693
  • 1
  • 4
  • 20
0

The second approach comes from C/C++. When you write if (user = null) instead of if (user == null) in C you'll get a logical error, but not a compilation one, and such an error may be really difficult to detect.

But in case you write if(null = user) instead of if(null == user) you'll get a compilation error as you cannot assign a variable to null. That's why reversed order of arguments is often used there.

However in Java this doesn't make much sense as Java compiler won't take a if(user = null) expression, so you can use any approach. The first one is more widespread though

svz
  • 4,516
  • 11
  • 40
  • 66