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

Java8Java Programming Object Oriented Programming

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
raja
Published on 26-Apr-2018 08:57:30
Advertisements