How to Check if a String is Empty or Null in Java Program

How to Check if a String is Empty or Null in java Program

Java Methods

  • In Java, a method is a block of code that performs a specific task and can be called from other parts of the program. Methods help to organize code and reduce duplication.
    Methods can take input parameters and can return a value.
  • You must be familiar with following topics to understand the correspond example. Java if…else Statement, Java Methods, Java String isEmpty(),Java String trim().

To know more about Java Program to check if a string is empty or null in Java, Read the complete article.

Steps to check if a string is empty or null in Java

  • Here are the steps to check if a string is empty or null in Java:, you can follow these steps:
    • Check for null: Use the if statement and the equality operator == to check if the string variable is null.

    • Check for empty: Use the if statement and the .length() method to check if the string length is zero.

    • Check for both: You can combine both checks using the && operator to make sure the string is not null and not empty.

Let’s look at a Java Program to check if a string is empty or null in Java to perform certain operations.

Example 1: Java Program to check if a string is empty or null in Java

Run
public class Main {
  public static void main(String[] args) {
    String str = "";  // change the value of str to test different cases

    // check if str is null
    if (str == null) {
      System.out.println("str is null");
    }
    // check if str is empty
    else if (str.length() == 0) {
      System.out.println("str is empty");
    }
    // str is not null and not empty
    else {
      System.out.println("str is not empty or null");
    }
  }
}

Output

str is empty

Example 2 : Java Program to check if a string is empty or null in Java

Run
public class Main {
  public static void main(String[] args) {
    String str = "";  // change the value of str to test different cases

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

Output

An error occurred while reading the file: path/to/file (No such file or directory)

Example 3: Java Program to Check if String with spaces is Empty or Null 

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

    if (str == null || str.trim().isEmpty()) {
      System.out.println("String is empty or null");
    } else {
      System.out.println("String is not empty or null");
    }
  }
}

Output

String is empty or null

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription