How to test String is null or empty?

Java 8Object Oriented ProgrammingProgramming

We can verify whether the given string is empty using the isEmpty() method of the String class. This method returns true only if length() is 0.


Live Demo

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str = "tutorialspoint";
      // prints length of string
      System.out.println("length of string = " + str.length());

      // checks if the string is empty or not
      System.out.println("is this string empty? = " + str.isEmpty());


length of string = 14
is this string empty? = false
Published on 12-Feb-2018 06:16:24