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))
Answer from Jon Skeet on Stack OverflowNo, 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))
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.
Videos
You're trying to call the isEmpty() method on a null reference (as List test = null;). This will surely throw a NullPointerException. You should do if(test!=null) instead (checking for null first).
The method isEmpty() returns true, if an ArrayList object contains no elements; false otherwise (for that the List must first be instantiated that is in your case is null).
You may want to see this question.
I would recommend using Apache Commons Collections:
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/CollectionUtils.html#isEmpty-java.util.Collection-
which implements it quite ok and well documented:
/**
* Null-safe check if the specified collection is empty.
* <p>
* Null returns true.
*
* @param coll the collection to check, may be null
* @return true if empty or null
* @since Commons Collections 3.2
*/
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}
If you use the Apache Commons Collections library in your project, you may use the CollectionUtils.isEmpty(...) and MapUtils.isEmpty(...) methods which respectively check if a collection or a map is empty or null (i.e. they are "null-safe").
The code behind these methods is more or less what user @icza has written in his answer.
Regardless of what you do, remember that the less code you write, the less code you need to test as the complexity of your code decreases.
That is the best way to check it. You could write a helper method to do it:
public static boolean isNullOrEmpty( final Collection< ? > c ) {
return c == null || c.isEmpty();
}
public static boolean isNullOrEmpty( final Map< ?, ? > m ) {
return m == null || m.isEmpty();
}
The empty string is a string with zero length. The null value is not having a string at all.
- The expression
s == nullwill returnfalseif s is an empty string. - The second version will throw a
NullPointerExceptionif the string is null.
Here's a table showing the differences:
+-------+-----------+----------------------+
| s | s == null | s.isEmpty() |
+-------+-----------+----------------------+
| null | true | NullPointerException |
| "" | false | true |
| "foo" | false | false |
+-------+-----------+----------------------+
Strings that have assigned with "", don't contain any value but are empty (length=0), Strings that are not instantiated are null.
I just completed MOOC, Java part II, exercise 19 for week 9 and found that the order I placed name.isEmpty() or name == null was the key to get it's tests to pass.
The test which failed was one that tested a null but I don't understand why it matters since it's an or statement.
Thank You
So this fails
public Person(String name, int age) {
if ( name.isEmpty() || name == null || name.length() > 40) {
throw new IllegalArgumentException("Name is an invalid length.");
} else {
this.name = name;
}
if ((age < 0) || (age > 120)) {
throw new IllegalArgumentException("Age is not a valid range");
} else {
this.age = age;
}
}But this passes
public Person(String name, int age) {
if ( name == null || name.isEmpty() || name.length() > 40) {
throw new IllegalArgumentException("Name is an invalid length.");
} else {
this.name = name;
}
if ((age < 0) || (age > 120)) {
throw new IllegalArgumentException("Age is not a valid range");
} else {
this.age = age;
}
}What about isEmpty() ?
if(str != null && !str.isEmpty())
Be sure to use the parts of && in this order, because java will not proceed to evaluate the second part if the first part of && fails, thus ensuring you will not get a null pointer exception from str.isEmpty() if str is null.
Beware, it's only available since Java SE 1.6. You have to check str.length() == 0 on previous versions.
To ignore whitespace as well:
if(str != null && !str.trim().isEmpty())
(since Java 11 str.trim().isEmpty() can be reduced to str.isBlank() which will also test for other Unicode white spaces)
Wrapped in a handy function:
public static boolean empty( final String s ) {
// Null-safe, short-circuit evaluation.
return s == null || s.trim().isEmpty();
}
Becomes:
if( !empty( str ) )
Use org.apache.commons.lang.StringUtils
I like to use Apache commons-lang for these kinds of things, and especially the StringUtils utility class:
import org.apache.commons.lang.StringUtils;
if (StringUtils.isNotBlank(str)) {
...
}
if (StringUtils.isBlank(str)) {
...
}
The main benefit of "".equals(s) is you don't need the null check (equals will check its argument and return false if it's null), which you seem to not care about. If you're not worried about s being null (or are otherwise checking for it), I would definitely use s.isEmpty(); it shows exactly what you're checking, you care whether or not s is empty, not whether it equals the empty string
String.equals("") is actually a bit slower than just an isEmpty() call. Strings store a count variable initialized in the constructor, since Strings are immutable.
isEmpty() compares the count variable to 0, while equals will check the type, string length, and then iterate over the string for comparison if the sizes match.
So to answer your question, isEmpty() will actually do a lot less! and that's a good thing.