Java String isEmpty example

String’s isEmpty method checks if length of string is 0 or not. It returns true if length of String is 0 else return false.

Method Signature:

public boolean isEmpty()

String isEmpty Example:

package org.arpit.java2blog;

public class StringIsEmptyExample {

 /*
  * @Author : Arpit Mandliya
  */
 public static void main(String[] args)
 {
  String str1="";
  String str2="java2blog";
  String str3=null;

  System.out.println("str1 isEmpty: "+str1.isEmpty() );
  System.out.println("str2 isEmpty: "+str2.isEmpty() );
  // str3.isEmpty will throw NullPointerException
  //System.out.println("str3 isEmpty: "+str3.isEmpty() );
 }
}

When you run above program, you will get below output:

str1 isEmpty: true
str2 isEmpty: false
[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.