Algorithm


  1. Input:

    • String variable inputString
  2. Algorithm: a. Check if the string is null:

    • If true, the string is considered empty.
    • If false, proceed to the next step.

    b. Check if the string is empty (has zero length):

    • If true, the string is empty.
    • If false, the string is neither empty nor null.
  3. Output:

    • Boolean value indicating whether the string is empty or null.

 

Code Examples

#1 Code Example- Check if String is Empty or Null

Code - Java Programming

class Main {
  public static void main(String[] args) {
    
    // create null, empty, and regular strings
    String str1 = null;
    String str2 = "";
    String str3 = "  ";

    // check if str1 is null or empty
    System.out.println("str1 is " + isNullEmpty(str1));

    // check if str2 is null or empty
    System.out.println("str2 is " + isNullEmpty(str2));

    // check if str3 is null or empty
    System.out.println("str3 is " + isNullEmpty(str3));
  }

  // method check if string is null or empty
  public static String isNullEmpty(String str) {

    // check if string is null
    if (str == null) {
      return "NULL";
    }

    // check if string is empty
    else if(str.isEmpty()){
      return "EMPTY";
    }

    else {
      return "neither NULL nor EMPTY";
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
str1 is NULL
str2 is EMPTY
str3 is neither NULL nor EMPTY

#2 Code Example- Check if String with spaces is Empty or Null

Code - Java Programming

class Main {
  public static void main(String[] args) {
    
    // create a string with white spaces
    String str = "    ";

    // check if str1 is null or empty
    System.out.println("str is " + isNullEmpty(str));
  }

  // method check if string is null or empty
  public static String isNullEmpty(String str) {

    // check if string is null
    if (str == null) {
      return "NULL";
    }

    // check if string is empty
    else if (str.trim().isEmpty()){
      return "EMPTY";
    }

    else {
      return "neither NULL nor EMPTY";
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
str is EMPTY
Advertisements

Demonstration


Java Programing Example to Check if a String is Empty or Null-DevsEnv