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?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges