Program to check if the String is Empty in Java Last Updated : 28 Jul, 2022 Comments Improve Suggest changes 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 Program to check if the String is Empty in Java C code_r Follow Improve Article Tags : Strings Java DSA Java-String-Programs Practice Tags : JavaStrings 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 Program to check if first and the last characters of string are equal We are given a string, we need to check whether the first and last characters of the string str are equal or not. Case sensitivity is to be considered. Examples : Input : university Output : Not Equal Explanation: In the string "university", the first character is 'u' and the last character is 'y', 4 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 Check if a String Contains only Alphabets in Java using Regex Regular Expressions or Regex is an API for defining String patterns that can be used for searching, manipulating, and editing a text. It is widely used to define a constraint on strings such as a password. Regular Expressions are provided under java.util.regex package. For any string, here the task 2 min read String Handling with Apache Commons' StringUtils Class in Java The Apache Commons Lang library is a popular third-party library for working with Strings in Java, and the StringUtils class is a key part of this library. StringUtils is a utility class that provides a wide range of String manipulation methods that are not available in the standard Java String clas 9 min read Check length of a string is equal to the number appended at its last Given a string that (may) be appended with a number at last. You need to find whether the length of string excluding that number is equal to that number. For example for "helloworld10", answer is True as helloworld consist of 10 letters. Length of String is less than 10, 000. Examples : Input: str = 11 min read Like