String isEmpty() Java with Examples

String isEmpty() Java | In the String class of Java, the isEmpty() method is given to check whether the String is empty or not. If the string is empty then this method returns false else it returns true. This method was given in Java 1.6.

The internal implementation of the Java String isEmpty() method,

public boolean isEmpty() {
   return value.length == 0;
}

Method declaration:- public boolean isEmpty()
Return value of String isEmpty() method:-

  • true:- If the length of the string is 0.
  • false:- If the length of the string is greater than 0.

This method returns true if the String doesn’t have a character, and it returns false if at least 1 character is available in this string object.

Also see:- isBlank() method in Java String class, isBlank() vs isEmpty() in Java String, Different ways to check if String is Empty in Java

Program to demonstrate String isEmpty() Java,

public class Test {

   public static void main(String[] args) {

      String s1 = "";
      System.out.println(s1.isEmpty());

      String s2 = " ";
      System.out.println(s2.isEmpty());

      String s3 = "java";
      System.out.println(s3.isEmpty());

      String s4 = new String();
      System.out.println(s4.isEmpty());

      String s5 = new String("");
      System.out.println(s5.isEmpty());

      String s6 = new String(" ");
      System.out.println(s6.isEmpty());

      String s7 = new String("java");
      System.out.println(s7.isEmpty());
   }
}

The output of the above program:-

true
false
false
true
true
false
false

In the string literal "" there is no valid character it contains only ‘\0’, and the length of this string literal is 0 therefore isEmpty() method returns true. But the string literal " " contains one space that is a valid character hence the length of this string literal is 1 and the isEmpty() method returns false.

From the above program,

ValueResult
""true
" "false
"java"false
new String()true
new String("")true
new String(" ")false
new String("java")false

Let us demonstrate Java String isEmpty() method with one more example. This time, we will get input from the end user through the Scanner class.

import java.util.Scanner;

public class Test1 {

   public static void main(String[] args) {

      Scanner scan = new Scanner(System.in);

      System.out.println("Enter three Strings in three lines,");
      String s1 = scan.nextLine(); // directly press enter key
      String s2 = scan.nextLine(); // enter blank spaces or tabs
      String s3 = scan.nextLine(); // enter normal string

      System.out.println(s1.isEmpty() + " " + s1.length());
      System.out.println(s2.isEmpty() + " " + s2.length());
      System.out.println(s3.isEmpty() + " " + s3.length());
   }
}

Output:-

Enter three Strings in three lines,
->(Here directly enter key was pressed)
->(Here one space was entered)
Know Program
true 0
false 1
false 12

In this program, The string s1 is an empty string and it is similar to the previous program String s5 = new String(""); Notice that it is different from String s4 = new String();

MCQ on String isEmpty() Java

Based on the above-discussed points find the valid answers for the given questions on String isEmpty() Java.

Q1) Find the output of the below program.

package com.kp.test;
public class Q1 {

   public static String s1;

   public static void main(String[] args) {
      System.out.println(s1.isEmpty());
   }
}

a) true
b) false
c) Compile time error
d) NullPointerException

View Answer Answer:- d) NullPointerException

Explanation:- If the string is not initialized then we can’t perform any operation on it because there is not any String object available here. Due to this reason, we will get an Exception in thread “main” java.lang.NullPointerException: Cannot invoke “String.isEmpty()” because “com.kp.test.Test.s1” is null at com.kp.test.Test.main(Test.java:6)

Q2) Find the output of the below program.

package com.kp.test;
public class Q2 {
   public static void main(String[] args) {
      String s2;
      System.out.println(s2.isEmpty());
   }
}

a) true
b) false
c) Compile time error
d) NullPointerException

View Answer Answer:- c) Compile time-error

Explanation:- In this program, before running compile time itself gets an error because the local variable is not initialized. All the local variables must be initialized before using it. Hence we get a compile-time error: variable s2 might not have been initialized.

Q3) Find the output of the below program.

package com.kp.test;
public class Q3 {
   public static void main(String[] args) {
      String s3 = null;
      System.out.println(s3.isEmpty());
   }
}

a) true
b) false
c) Compile time error
d) NullPointerException

View Answer Answer:- d) NullPointerException

Explanation:- The string is not initialized, and we are trying to call the isEmpty() method with null reference. Due to this reason, we will get a run time error:- java.lang.NullPointerException

Q4) Find the output of the below program.

package com.kp.test;
public class Q4 {
   public static void main(String[] args) {
      String s4 = "null";
      System.out.println(s4.isEmpty());
   }
}

a) true
b) false
c) Compile time error
d) NullPointerException

View Answer Answer:- b) false

Explanation:- In this program, “null” is a String literal with characters ‘n’, ‘u’, ‘l’, ‘l’. It is a simple string like “Hi”, “Hello”. Hence s4.isEmpty() gives result as false.

The null and “null” are completely different things. The string s3 has null, it is a reference that can be used for initializing any reference variable. The compiler will not give any error if the string is not null but JVM will give an exception when we try to excess string data. The string reference is null so JVM gives an exception. Hence s2.isEmpty() gives NullPointerException in Q2.

But “null” is a String literal with 4 characters ‘n’, ‘u’, ‘l’, ‘l’. It is a simple string literal like “Hi”, “Hello”, “java”, and e.t.c. Hence s4.isEmpty() gives the result as false in Q4.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *