String comparisions in java

Go To StackoverFlow.com

3

Possible Duplicate:
A better way to compare Strings which could be null

I have an if condition which looks like this :

if( !str1.equals(str2) )
{
    ---
    ---
    ---
}

where str1 and str2 are two string objects.

There are chances that str1 might be null , so is the below code equivalent to the above, along with handling the null check?

if( !(str1==null ? str2==null : str1.equals(str2)) )
{
    ---
    ---
    ---
}

Thanks!

2012-04-04 17:13
by user1168608
Depends on how you imagine the null check being handled in the bit of code that is just in your head - David Heffernan 2012-04-04 17:15
If str1 is null then if you will do str1.equals(); you will get NullPointerException. For more detail check this http://stackoverflow.com/questions/2601978/how-to-check-if-my-string-is-equal-to-nul - Vyoma 2012-04-04 17:21
Your condition looks fine. You could rewrite it as if( str1==null ? str2!=null : !str1.equals(str2) ) which might be easier to read - NullUserException 2012-04-04 18:19


4

Yes, that will lead to the same result.

To be a bit more specific:

  1. If str1 isn't null, it's exactly the same, since it just passes through the ternary check to the same expression as before
  2. If str1 is null, it then becomes a check to see if str2 is also null.

And since you have the whole ternary expression wrapped up with the ! out front, that behaves the same as before.

If you wanted to be a bit more clear, you could make str2==null into an actual comparison between str1 and str2: str1==str2. Since one of the values is already null, it doesn't matter that it's a referential check instead of a proper string equality check, and ends up being a bit more clear in the code (to me, anyways)

As others have mentioned, however, the Apache Commons library already includes this null-safe equality capability, but it does require a rather substantial library inclusion. On the other hand, many feel that the Apache Commons functionality should be effectively considered a part of Java itself, so you can decide for yourself if you want the extra dependency.

Lastly, the functionality isn't technically equivalent, since the default .equals() method will throw a NullPointerException, while your equality check code won't. If that is the behavior you were looking for (which I assume it is), then you're fine, but it is something to be aware of.

2012-04-04 17:15
by cdeszaq


5

Assuming you want two nulls to be equal, sure. Depending on your method, NullPointerException may be the correct response. You can save yourself a lot of typing by getting to know

org.apache.commons.lang.StringUtils

!StringUtils.equals(str1, str2); handles the nulls for you in the same way.

2012-04-04 17:17
by Affe
Apache string utils can be a huge help to any project, why reinvent the wheel - Mike McMahon 2012-04-04 17:18
+1 for suggesting apache commons StringUtils before I got there. StringUtils is very useful for exactly this kind of thing, comparisons that handle nulls correctly - Tom Quarendon 2012-04-04 17:18
It can also introduce a large dependency when a very small amount of code would suffice.. - maerics 2012-04-04 17:20


5

If you're using Java 7:

java.util.Objects.equals(str1, str2);
2012-04-04 17:18
by Joel


2

It looks correct, if you consider nulls equal, and allow str2 to be null. But it would be better for you not to beleive anybody, but write a test for all possible cases.

2012-04-04 17:18
by max.shmidov


1

Using the ternary operator is a really silly way of doing that check, and it makes your could frustrating to read. Just use an AND.

if(str1 != null && !str1.equals(str2))
{
    ---
    ---
    ---
}

You don't need to import a whole new library to do a null check.

2012-04-04 17:18
by Hunter McMillen
+1 amen for the sanity of a simple solution instead of using a library.. - maerics 2012-04-04 17:23
This doesn't check to see if str2 is equal (ie. null == null) to str1. Instead, this code says that if str1 == null, then it can't be equal to str2. This answer also doesn't answer the question of if the two options are equal - cdeszaq 2012-04-04 17:26
@cdeszaq The OP wants to go into the if statement if str1 and str2 are not equal. That is what his very first if statement does. All this code does is ensures that str1 is also not null. You will notice if you re-read the post, that the OP isn't concerned with str2 being null - Hunter McMillen 2012-04-04 17:55
With the code you provided, if str2 == null and str1 == null, you would not enter the block, even though str2 == str1. So, your code fails the test where both inputs are null, since your code does not consider them equal, even though they are - cdeszaq 2012-04-04 18:02
@HunterMcMillen The OP wants to check if str1 and str2 are not equal. They only mention str1 because if str1 is null, str1.equals(str2) will cause an NPE. They clearly are concerned about str2, as seen in the second piece of code in the question. Your answer fails when both are null - NullUserException 2012-04-04 18:14


0

Yes, your code will enter the if block only if the strings are unequal (either only one of them is null or both of them are not null and different).

Another way to do it is

if( (str1 == null && str2 != null) || !str1.equals(str2) ){
    // Only executes when strings are unequal
}

It's a matter of taste, some people are allergic to ternary operators. (I like them).

2012-04-04 17:39
by trutheality
Ads