Java program to find whether the given string is empty or null



The isEmpty() method of the String class returns true if the length of the current string is 0.

Example

import java.util.Scanner;
public class StringEmptyOrNull {
   public static void main(String[] args) {
      System.out.println("Enter a string value ::");
      Scanner sc = new Scanner(System.in);
      String str = sc.nextLine();

      if(str.isEmpty()&& str==null ){
         System.out.println("Given string is empty or null");
      }else{
         System.out.println("Given string is not empty or null");
      }
   }
}

Output 1

Enter a string value ::
Hi welcome to tutorialspoint
Given string is not empty or null

Output 2

Enter a string value ::
Given string is empty or null
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements