1/* You can use Apache Commons */
2StringUtils.isEmpty(str)
3
4/* which checks for empty strings and handles null gracefully. */
5
6/* HINT */
7/* the shorter and simler your code, the easier it is to test it */
1public class Null {
2
3 public static boolean isNullOrEmpty(String str) {
4 if(str != null && !str.isEmpty())
5 return false;
6 return true;
7 }
8}
1if(str != null && !str.isEmpty())
2Be 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.
1if (myString == null || myString.equals(""))
2 throw new IllegalArgumentException("empty string");