Program to check if the String is Empty in Java Last Updated : 28 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string str, the task is to check if this string is empty or not, in Java. Examples: Input: str = "" Output: True Input: str = "GFG" Output: False Approach 1: Get the String to be checked in strWe can simply check if the string is empty or not using the isEmpty() method of String class Syntax:if (str.isEmpty())Print true if the above condition is true. Else print false. Below is the implementation of the above approach: Java // Java Program to check if // the String is empty in Java class GFG { // Function to check if the String is empty public static boolean isStringEmpty(String str) { // check if the string is empty or not // using the isEmpty() method // and return the result if (str.isEmpty()) return true; else return false; } // Driver code public static void main(String[] args) { String str1 = "GeeksforGeeks"; String str2 = ""; System.out.println("Is string \"" + str1 + "\" empty? " + isStringEmpty(str1)); System.out.println("Is string \"" + str2 + "\" empty? " + isStringEmpty(str2)); } } Output:Is string "GeeksforGeeks" empty? false Is string "" empty? true Approach 2: Using length() method If the length of the string is zero then the string is empty. Java // Java Program to check if // the String is empty in Java class GFG{ public static void main(String[] args) { String str1 = "GeeksforGeeks"; String str2 = ""; boolean res1=false; boolean res2=false; if(str1.length()==0){ res1=true; } if(str2.length()==0){ res2=true; } System.out.println("Is string \"" + str1+ "\" empty? "+ res1); System.out.println("Is string \"" + str2+ "\" empty? "+ res2); } } OutputIs string "GeeksforGeeks" empty? false Is string "" empty? true Comment More infoAdvertise with us Next Article Java String isEmpty() Method with Example C code_r Follow Improve Article Tags : Java Java-String-Programs Practice Tags : Java Similar Reads Program to check if a String in Java contains only whitespaces Given a string str, the task is to check if this string contains only whitespaces or some text, in Java. Examples: Input: str = " " Output: True Input: str = "GFG" Output: False Approach: Get the String to be checked in str We can use the trim() method of String class to remove the leading whitespac 2 min read Compare two Strings in Java String in Java are immutable sequences of characters. Comparing strings is the most common task in different scenarios such as input validation or searching algorithms. In this article, we will learn multiple ways to compare two strings in Java with simple examples.Example:To compare two strings in 4 min read Java String isEmpty() Method with Example In Java, the String isEmpty() method checks if a string is empty (length is zero). This method returns true if the string is empty and false otherwise. It is useful for validating strings in our applications.In this article, we will learn how to use the isEmpty() method in Java along with examples t 3 min read Check if a String Contains only Alphabets in Java In Java, to check if a string contains only alphabets, we have to verify each character to make sure it falls within the range of valid alphabetic characters. There are various ways to check this in Java, depending on requirements.Example: The most common and straightforward approach to validate if 4 min read StringWriter equals() method in Java with Example The Java.io.StringWriter.equals(Object obj) method of StringWriter class in Java is used to check whether the two instances of StringWriter are equal or not. It returns a boolean stating whether they are equal or not. Signature: public boolean equals(StringWriter second_StringWriter) Syntax: first_S 2 min read Null Pointer Exception in Java A NullPointerException in Java is a RuntimeException. It occurs when a program attempts to use an object reference that has the null value. In Java, "null" is a special value that can be assigned to object references to indicate the absence of a value.Reasons for Null Pointer ExceptionA NullPointerE 5 min read Like