Reputation: 9624
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.
Upvotes: 16
Views: 26523
Reputation: 1
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;
}
Upvotes: 0
Reputation: 4211
Strings that have assigned with "", don't contain any value but are empty (length=0), Strings that are not instantiated are null.
Upvotes: 3
Reputation: 3710
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.
Upvotes: 0
Reputation: 838226
The empty string is a string with zero length. The null value is not having a string at all.
s == null
will return false
if s is an empty string.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 |
+-------+-----------+----------------------+
Upvotes: 51
Reputation: 28727
isEmpty checks for String "". Best practise is to check:
if (str != null && !str.isEmpty() {
// process string
}
Upvotes: 0
Reputation: 7521
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 }
Upvotes: 0
Reputation: 272287
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. i.e.
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
Upvotes: 2
Reputation: 240900
isEmpty()
checks for empty string ""
,
it will throw NullPointerException
if you invoke isEmpty()
on null
instance
Upvotes: 1