OneBite.Dev - Coding blog in a bite size

check if a string is empty in java

Code snippet on how to check if a string is empty in java

  String str = "";
  if (str.length() == 0) {
    System.out.println("The string is empty.");
  }

This code checks if a string is empty in Java. The string is declared as an empty string. Then, an if-statement checks if the length of the string is 0 after the declaration. If it is 0, then it prints out the phrase “The string is empty”. If it has anything inside the string, the length will be greater than 0 and the phrase will not be printed.

java