views:

512

answers:

11

Possible Duplicates:
Gracefully avoiding NullPointerException in Java
Multilingual fields in DB tables

Exact duplicate

http://stackoverflow.com/questions/963936/gracefully-avoiding-nullpointerexception-in-java

What do you like more? I just hate seeing the last one. It just seems backwards.

String randomtext = "stack overflow";

if(randomtext.equals("stack overflow"))
{
      //do something
}

or

String randomtext = "stack overflow";

if("stack overflow".equals(randomtext))
{
     //do something
}
A: 

I'd say the general opinion would go toward the first, it's more readable.

marcgg
+13  A: 

Contrary to what some people think, these two are not functionally equivalent. The first one will throw a NullPointerException if the randomtext is null while the second one won't. This is why I'd choose the latter.

Mehrdad Afshari
+1 - This is a very good point that I didn't think about but, you must admit that the possibility of an exception depends on the implementation.
Frank V
I actually didn't think of that. That's a good point!
Tommy
A: 

Depending on the implementation this could be personal preference.

Now, if there is a possibility that randomtext will be null, I think that Merdad's post is important.

But given your example,

String randomtext = "stack overflow";

if(randomtext.equals("stack overflow"))
{
      //do something
}

it won't matter because in this case randomtext is always going to be "stack overflow".

If you want to know which is "better", then it depends on why it is better and that is up to you.

Frank V
+1  A: 

I prefer the first. I like to write conditions as: Test_Subject <-> Control_Value

KM
+1  A: 

If the string variable can be null, you always want to make the literal go first.

"stack overflow".equals(randomtext)

will never cause a NullPointerException, but

randomtext.equals("stack overflow")

will, if randomtext == null.

For that reason, although I like the variable.equals(literal), I used the former.

lavinio
A: 

I'd rather

String randomText = "stack overflow";
String otherRandomText = "something else";

if(randomText.equals(otherRandomText))
{
   //do something
}
mbehan
The question is why? Can you explain your point of view -- I'm interested.... Keep in mind that `otherRandomText` will then take memory to hold which is probably why your answer is getting voted down.
Frank V
A: 

I prefer the first as well. As Mehrdad points out, the second has a technical advantage (not throwing an exception on null), but I think it is less natural.

I feel the same way about if (null != myVar) - it just doesn't read right, to me. Null - or the literal string - never changes, so asking if it is equal to something - although meaningful and literally correct - just seems wrongheaded.

Carl Manaster
A: 

It depends on your purpose.

First of all, you shouldn't have text in your programs anyway. This is standardard practice in case you have to change that text in multiple places. So really this should be a constant or enum.

With that in mind, you would have String randomtext = "stack overflow"; const String stackoverflow = "stack overflow";

Now are you asking which looks better between randomtext.equals(stackoverflow) and stackoverflow.equals(randomtext)

I don't know if it really matters. I would usually put the actual test case in the ().

Justin Balvanz
A: 

In C, or C++, the latter kind of structure might be preferred, because it's easy to confuse the following two

if (randomNumber == 5) if (randomNumber = 5)

The second one is syntactically valid, but is an assignment statement that always returns true, while setting randomNumber to 5. That's probably not what was intended. If you make a habit of always writing your C++ comparisons using the structure

if (5 == randomNumber)

Then you're safer, because if you accidentally write = instead of ==, the compiler will scream at you.

This argument doesn't hold so much water for the original posted example, but it does explain why some programmers got into the habit of putting the constant first when doing comparisons, even though most people think the other way around seems more natural. Old habits die hard.

+1  A: 

I tend to check for null:

if(randomtext != null && randomtext.equals("stack overflow"))
{
      //do something
}
Chris Mazzola
A: 

I prefer org.apache.commons.lang.StringUtils.equals() and all of it's related String functions. Then it doesn't matter which is null, it's handled for you.

I believe all of the methods in StringUtils are null-safe.

Chris Kessel