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!
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
if( str1==null ? str2!=null : !str1.equals(str2) ) which might be easier to read - NullUserException 2012-04-04 18:19
Yes, that will lead to the same result.
To be a bit more specific:
str1 isn't null, it's exactly the same, since it just passes through the ternary check to the same expression as beforestr1 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.
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.
If you're using Java 7:
java.util.Objects.equals(str1, str2);
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.
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.
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
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
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
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).