Check if a String is Empty or Null in Java



In this article, we will understand how to check if a string is empty or null in Java. The string is a datatype that contains one or more characters and is enclosed in double quotes(" "). We'll start by learning to detect these cases directly within the main method, and then we'll see how to achieve the same result using encapsulation for better code organization and reusability.

Problem Statement

Write a program in Java to check if a string is empty or null. Below is a demonstration of the same ?

Input

Input string: null

Output

The string is a null string

Different approaches

Following are the different approaches to checking if a string is empty or null ?

Using main() method

Following are the steps to check if a string is empty or null using the ?main' method ?

  • Create a class named Demo with the main method.
  • Define a string variable, input_string, and set it to null or any value you wish to check.
  • Print the value of input_string to the console for reference.
  • Check for Null or Empty:
    • Use an if condition to check if input_string is null. If true, print that it's a null string.
    • Use else if to check if the string is empty with input_string.isEmpty(). If true, print that it's an empty string.
  • If neither condition is met, print that the string is neither null nor empty.

Example

Here, we bind all the operations together under the ?main' method ?

public class Demo {
   public static void main(String[] args) {
      String input_string = null;
      System.out.println("The string is defined as: " +input_string);
      if (input_string == null) {
         System.out.println("\nThe string is a null string");
      }
      else if(input_string.isEmpty()){
         System.out.println("\nThe string is an empty string");
      } else {
         System.out.println("\nThe string is neither empty nor null string");
      }
   }
}

Output

The string is defined as: null

The string is a null string

Using encapsulation

Following are the steps to check if a string is empty or null using encapsulation ?

  • Create a class named Demo and add a static method isNullEmpty that takes a string parameter, input_string.
  • Check for Null or Empty:
    • In isNullEmpty, use an if statement to check if input_string is null. Print that it's a null string if true.
    • Use else if to check if the string is empty using input_string.isEmpty(). Print that it's an empty string if true.
    • Otherwise, print that the string is neither null nor empty.
  • Initialize and Test in main:
    • In the main, define a string variable input_string and set it to null.
    • Print the value of input_string.
  • Call the isNullEmpty method with input_string as an argument to check its state.

Example

Here, we encapsulate the operations into functions exhibiting object-oriented programming ?

public class Demo {
   static void isNullEmpty(String input_string) {
      if (input_string == null) {
         System.out.println("\nThe string is a null string");
      }
      else if(input_string.isEmpty()){
         System.out.println("\nThe string is an empty string");
      } else {
         System.out.println("\nThe string is neither empty nor null string");
      }
   }
   public static void main(String[] args) {
      String input_string = null;
      System.out.println("The string is defined as: " +input_string);
      isNullEmpty(input_string);
   }
}

Output

The string is defined as: null

The string is a null string
Updated on: 2024-11-08T22:31:26+05:30

919 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements