GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java String isEmpty() Method with Example - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java String isEmpty() Method with Example

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()); 
    }
}

Output
true
false

Table of Content

  • Syntax of isEmpty() Method
  • Java Programs to Demonstrate String isEmpty() method
    • Validate User Input using isEmpty()
    • Check Multiple Strings using isEmpty()
    • Using isEmpty() with Conditional Logic

Syntax of isEmpty() Method

boolean isEmpty()

Return Value:

  • It returns true if the string length is 0.
  • Returns false otherwise.

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.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.