简体   繁体   中英

Any difference between String = null and String.isEmpty?

Is there any difference when using a if-statement to check if the string is empty by using String = null or String.isEmpty() ?

ie:

public String name;

if(name == null)
{
    //do something
}

or

public String name;

if(name.isEmpty())
{
    //do something
}

if there is any different (including performance issues) please let me know.

The empty string is a string with zero length. The null value is not having a string at all.

  • The expression s == null will return false if s is an empty string.
  • The second version will throw a NullPointerException if the string is null.

Here's a table showing the differences:

+-------+-----------+----------------------+
| s     | s == null | s.isEmpty()          |
+-------+-----------+----------------------+
| null  | true      | NullPointerException |
| ""    | false     | true                 |
| "foo" | false     | false                |
+-------+-----------+----------------------+

The variable name isn't a String. It's a reference to a String.

Hence the null check determines if name actually references a String . If it does, then (and only then) can you perform a further check to see if it's empty. ie

String name = null;  // no string
String name = "";    // an 'empty' string

are two different cases. Note that if you don't check for nullness first, then you'll try and call a method on a null reference and that's when you get the dreaded NullPointerException

用“”赋值的字符串不包含任何值而是空的(长度=0),未实例化的字符串为空。

isEmpty() checks for empty string "" ,

it will throw NullPointerException if you invoke isEmpty() on null instance

If you apply this code:

if(name.isEmpty())
{
    //do something
}

when name is null, you'll get NullPointerException .

null checking shows you whether there is an object generally.
isEmpty checking shows you whether the content of existing String object is empty.

Look at source code of your java version.

For example, in openjdk-7: http://www.docjar.com/html/api/java/lang/String.java.html

  119       /** The count is the number of characters in the String. */
  120       private final int count;

  663       /**
  664        * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
  665        *
  666        * @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
  667        * <tt>false</tt>
  668        *
  669        * @since 1.6
  670        */
  671       public boolean isEmpty() {
  672           return count == 0;
  673       }

isEmpty checks for String "". Best practise is to check:

if (str != null && !str.isEmpty() {
   // process string
}

I had this issue this week while modifying some old JaVa code and i learn here that i must always make all those check. The answer is indeed correct but i find it hard to remember each time so i decide to make a little function that do it for me in 1 simple call.

whit this, you always get the answer you want :

      public boolean StringIsNull(String pi_sChaine)
        {  boolean bTrueOrFalse = true;

           if (pi_sChaine == null || pi_sChaine.isEmpty())
             { bTrueOrFalse = true; }
           else
             { bTrueOrFalse = false; }

           return bTrueOrFalse;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM