Java String isEmpty()

The syntax of the string isEmpty() method is:

string.isEmpty()

Here, string is an object of the String class.


isEmpty() Parameters

The isEmpty() method does not take any parameters.


isEmpty() Return Value

  • returns true if the string is empty (length is 0)
  • returns false if the string is not empty

Example: Java String isEmpty()

class Main {
  public static void main(String[] args) {

    String str1 = "Java Programming";
    String str2 = "";

    System.out.println(str1.isEmpty()); // false
    System.out.println(str2.isEmpty()); // true

  }
}

Note: A non-initialized string is not an empty string. If you use isEmpty() on a string that is not initialized, you will get an error.


Also Read:

Before we wrap up, let’s put your knowledge of Java String isEmpty() to the test! Can you solve the following challenge?

Challenge:

Write a function to check if a string is empty.

  • If the string str is empty, return "String is empty". Otherwise, return "String isn't empty".
  • For example, if str = "Hello World!", the expected output is "String isn't empty".
Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community