Can we rely on String.isEmpty for checking null condition on a String in Java?

JavaString

Java Problem Overview


I am passing an accountid as input from an XML file as shown, which will be parsed later and will be used in our code:

<accountid>123456</accountid>
<user>pavan</user>

The issue is that if nothing is passed (null value in accoutnid) is passed as accountid, I could not able to handle that situation in Java code. I tried this but I was not successful:

if (acct != null||acct==""||acct.equals("")) 
{
    // the above is not working 
}

I was able to handle this successfully using the following approach:

if(!acct.isEmpty())
{
   // thisis working 
}

Can we rely on the String.isEmpty() method for checking the null condition of a String? Is this valid?

Java Solutions


Solution 1 - Java

No, absolutely not - because if acct is null, it won't even get to isEmpty... it will immediately throw a NullPointerException.

Your test should be:

if (acct != null && !acct.isEmpty())

Note the use of && here, rather than your || in the previous code; also note how in your previous code, your conditions were wrong anyway - even with && you would only have entered the if body if acct was an empty string.

Alternatively, using Guava:

if (!Strings.isNullOrEmpty(acct))

Solution 2 - Java

Use StringUtils.isEmpty instead, it will also check for null.

Examples are:

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true
 StringUtils.isEmpty(" ")       = false
 StringUtils.isEmpty("bob")     = false
 StringUtils.isEmpty("  bob  ") = false

See more on official Documentation on String Utils.

Solution 3 - Java

You can't use String.isEmpty() if it is null. Best is to have your own method to check null or empty.

public static boolean isBlankOrNull(String str) {
    return (str == null || "".equals(str.trim()));
}

Solution 4 - Java

No, the String.isEmpty() method looks as following:

public boolean isEmpty() {
    return this.value.length == 0;
}

as you can see it checks the length of the string so you definitely have to check if the string is null before.

Solution 5 - Java

I think the even shorter answer that you'll like is: StringUtils.isBlank(acct);

From the documentation: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isBlank%28java.lang.String%29

isBlank
public static boolean isBlank(String str)
Checks if a String is whitespace, empty ("") or null.

 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true
 StringUtils.isBlank(" ")       = true
 StringUtils.isBlank("bob")     = false
 StringUtils.isBlank("  bob  ") = false
 
Parameters:
str - the String to check, may be null
Returns:
true if the String is null, empty or whitespace

Solution 6 - Java

String s1=""; // empty string assigned to s1 , s1 has length 0, it holds a value of no length string

String s2=null; // absolutely nothing, it holds no value, you are not assigning any value to s2

so null is not the same as empty.

hope that helps!!!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser663724View Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavalradacherView Answer on Stackoverflow
Solution 3 - JavaVaanduView Answer on Stackoverflow
Solution 4 - JavaMartin NiederlView Answer on Stackoverflow
Solution 5 - JavaMaarten KollerView Answer on Stackoverflow
Solution 6 - Javasdinesh94View Answer on Stackoverflow