Comparing strings in java

Possible Duplicate:
Better way to compare strings that may be null

I have an if condition that looks like this:

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

      

where str1 and str2 are two string objects.

Is there a chance str1 could be null, since the code below is equivalent to the above, and also handling null check?

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

      

Thank!

+3


source to share


6 answers


Yes, it will lead to the same result.

To be more specific:

  • If str1

    not null, it is exactly the same as it just goes through the ternary test to the same expression as before
  • If str1

    null it becomes a check to see if it is str2

    also null.

And since you have the whole triple expression wrapped up with a front !

that behaves the same as before.



If you want to be more intuitive, you can make str2==null

the actual comparison between str1

and str2

: str1==str2

. Since one of the values ​​is already equal null

, it doesn't matter that it is a referential check instead of properly checking for string equality and ends up being still clearer in the code (to me, anyway)

As others have noted, the Apache Commons library already includes this zero-security capability, but it requires a fairly substantial library inclusion. On the other hand, many believe that the Apache Commons functionality should be effectively treated as part of Java itself, so you can decide for yourself if you want an additional dependency.

Finally, the functionality is not technically equivalent, as the default method .equals()

will generate NullPointerException

, while your equality checking code will not. If this is the behavior you were looking for (I suppose it is), then you are fine, but it is something to be aware of.

+4


source


Assuming you want the two zeros to be equal, of course. Depending on your method, NullPointerException might be the correct answer. You can save a lot of information by reading

org.apache.commons.lang.StringUtils

      



!StringUtils.equals(str1, str2);

handles zeros for you in the same way.

+5


source


If you are using Java 7:

java.util.Objects.equals(str1, str2);

      

+5


source


It looks right if you consider it null

equal and allow it to str2

be null

. But it would be better if you didn't believe anyone, but write a test for all possible cases.

+2


source


Using the ternary operator is a really silly way to do this check, and it makes it hard to read. Just use AND.

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

      

You don't need to import the entire new library to do null check.

+1


source


Yes, your code will enter the block if

only if the strings are not equal (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 triple operators. (I like them).

0


source







All Articles