Java String isEmpty() Method

      No Comments on Java String isEmpty() Method




Java String isEmpty() method checks whether a String is empty or not.

Signature of isEmpty() Method:

boolean isEmpty()

The internal implementation of String says that If the length of a String is length is 0 then it is considered as empty string otherwise it is not considered as empty string.

Internal Implementation of String:

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

Let’s take an example to illustrate Java String isEmpty() Method.

Example : Java String isEmpty() Method

package com.masterjavatutorial;

public class Test {
	public static void main(String[] args) {
		String str ="";
		int length = str.length();
		System.out.println("Length of str ="+length); 
		boolean isEmpty = str.isEmpty();
		if(isEmpty){
		    System.out.println("str is empty");
		}else{
			System.out.println("str is not empty");
		}

		String str1 = "helloworld";
		int length1 = str1.length();
		System.out.println("Length of str1 ="+length1); 
		boolean isEmpty1 = str1.isEmpty();
		if(isEmpty1){
		    System.out.println("str1 is empty");
		}else{
			System.out.println("str1 is not empty");
		}
	
	}

}

output of the above program is as below:

Length of str =0
str is empty
Length of str1 =10
str1 is not empty