Sunday, January 26, 2014

Test if a string is not null and empty

Source:
public class EmptyString {
   public static void main(String[] args) {

      String str = new String();

      if( (str != null) && (str.isEmpty()) ) {
        System.out.println("str is not null and empty");
      } else {
        System.out.println("str is either null or not empty");
      }
   }
}

Output:
   $ java EmptyString 
   str is not null and empty

Blog Archive