Java String isEmpty() Method with Example (original) (raw)

Last Updated : 20 Nov, 2024

In Java, the **String isEmpty() method checks if a string is empty (length is zero). This method returns true if the string is empty and false otherwise. It is useful for validating strings in our applications.

In this article, we will learn **how to use the isEmpty() method in Java along with examples to demonstrate its functionality.

**Example:

In this example, we will see whether a given string is empty.

Java `

// Java program to demonstrate isEmpty() method import java.util.*;

public class StringIsEmpty {

public static void main(String[] args) {

    // Declare and initialize strings
    String s1 = "";
    String s2 = "Java";

    // Check if the strings are empty
    System.out.println("" + s1.isEmpty()); 
    System.out.println("" + s2.isEmpty()); 
}

}

`

Table of Content

**Syntax of isEmpty() Method

boolean isEmpty()

**Return Value:

Java Programs to Demonstrate String isEmpty() method

1. Validate User Input using isEmpty()

To ensure that the user input is not empty before processing it, we can use **isEmpty() method of String class.

**Example:

Java `

// Java program to validate user input import java.util.Scanner;

public class StringIsEmpty {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.print("Enter a username: ");

    // Check if the Scanner has input available
    if (sc.hasNextLine()) {
        String a = sc.nextLine();

        // Check if the username is empty
        if (a.isEmpty()) {
            System.out.println("Error: Username cannot be empty.");
        } else {
            System.out.println("Welcome, " + a + "!");
        }
    } else {
        System.out.println("No input provided.");
    }

    // Close the scanner (optional)
    sc.close();
}

}

`

Output

Enter a username: No input provided.

2. Check Multiple Strings using isEmpty()

We can use isEmpty() method to validate multiple strings.

**Example:

Java `

// Java program to check multiple strings import java.util.*;

public class StringIsEmpty {

public static void main(String[] args) {

    String[] s = {"Java", "", "Programming"};

    for (int i = 0; i < s.length; i++) {
        System.out.println("Is string at index " + i + " empty? " + s[i].isEmpty());
    }
}

}

`

Output

Is string at index 0 empty? false Is string at index 1 empty? true Is string at index 2 empty? false

3. Using isEmpty() with Conditional Logic

To handle different scenarios, combine isEmpty() with conditional checks.

Java `

// Java program to demonstrate conditional logic with isEmpty() import java.io.*;

public class StringIsEmpty {

public static void main(String[] args) {

    String m = "";

    if (m.isEmpty()) {
      
        System.out.println("The message is empty.");
    } else {
        System.out.println("Message: " + m);
    }
}

}

`

Output

The message is empty.

**Explanation:

The above example checks if the **m string is empty using the **isEmpty() method and prints the message.