Java String isEmpty() Method with Example Last Updated : 20 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 to demonstrate its functionality.Example:In this example, we will see whether a given string is empty. Java // Java program to demonstrate isEmpty() method import java.util.*; public class StringIsEmpty { public static void main(String[] args) { // Declare and initialize strings String s1 = ""; String s2 = "Java"; // Check if the strings are empty System.out.println("" + s1.isEmpty()); System.out.println("" + s2.isEmpty()); } } Outputtrue false Table of ContentSyntax of isEmpty() Method Java Programs to Demonstrate String isEmpty() methodValidate User Input using isEmpty()Check Multiple Strings using isEmpty()Using isEmpty() with Conditional LogicSyntax of isEmpty() Method boolean isEmpty()Return Value:It returns true if the string length is 0.Returns false otherwise.Java Programs to Demonstrate String isEmpty() method1. Validate User Input using isEmpty()To ensure that the user input is not empty before processing it, we can use isEmpty() method of String class.Example: Java // Java program to validate user input import java.util.Scanner; public class StringIsEmpty { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a username: "); // Check if the Scanner has input available if (sc.hasNextLine()) { String a = sc.nextLine(); // Check if the username is empty if (a.isEmpty()) { System.out.println("Error: Username cannot be empty."); } else { System.out.println("Welcome, " + a + "!"); } } else { System.out.println("No input provided."); } // Close the scanner (optional) sc.close(); } } OutputEnter a username: No input provided. 2. Check Multiple Strings using isEmpty()We can use isEmpty() method to validate multiple strings.Example: Java // Java program to check multiple strings import java.util.*; public class StringIsEmpty { public static void main(String[] args) { String[] s = {"Java", "", "Programming"}; for (int i = 0; i < s.length; i++) { System.out.println("Is string at index " + i + " empty? " + s[i].isEmpty()); } } } OutputIs string at index 0 empty? false Is string at index 1 empty? true Is string at index 2 empty? false 3. Using isEmpty() with Conditional LogicTo handle different scenarios, combine isEmpty() with conditional checks. Java // Java program to demonstrate conditional logic with isEmpty() import java.io.*; public class StringIsEmpty { public static void main(String[] args) { String m = ""; if (m.isEmpty()) { System.out.println("The message is empty."); } else { System.out.println("Message: " + m); } } } OutputThe message is empty. Explanation:The above example checks if the m string is empty using the isEmpty() method and prints the message. Comment More infoAdvertise with us Next Article Java String isEmpty() Method with Example N Niraj_Pandey Follow Improve Article Tags : Java Java-Strings Java-lang package Java-Functions Practice Tags : JavaJava-Strings Similar Reads Set isEmpty() method in Java with Examples The java.util.Set.isEmpty() method is used to check if a Set is empty or not. It returns True if the Set is empty otherwise it returns False. Syntax: boolean isEmpty() Parameters: This method does not take any parameter Return Value: The method returns True if the set is empty else returns False. Be 1 min read Stack isEmpty() method in Java with Example The Java.util.Stack.isEmpty() method in Java is used to check and verify if a Stack is empty or not. It returns True if the Stack is empty else it returns False. Syntax: Stack.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Stackis empty 2 min read SortedSet isEmpty() method in Java with Examples The java.util.SortedSet.isEmpty() method is used to check if a SortedSet is empty or not. It returns True if the SortedSet is empty otherwise it returns False. Syntax: boolean isEmpty() Parameters: This method does not take any parameter. Return Value: The method returns True if the SortedSet is emp 1 min read SortedMap isEmpty() method in Java with Examples The isEmpty() method of SortedMap interface in Java is used to check if a map is having any entry for key and value pairs. If no mapping exists, then this returns true. Syntax: boolean isEmpty() Parameters: This method has no argument. Returns: This method returns True if the map does not contain an 2 min read Properties isEmpty() method in Java with Examples The isEmpty() method of Properties class is used to check if this Properties object is empty or not. Syntax: public boolean isEmpty() Parameters: This method accepts no parameters Returns: This method returns a boolean value stating if this Properties object is empty or not. Below programs show the 2 min read Map isEmpty() Method in Java with Examples This method is used to check if a map is having any entry for key and value pairs. If no mapping exists, then this returns true. Syntax: boolean isEmpty() Parameters: This method has no argument. Returns: This method returns True if the map does not contain any key-value mapping. Below programs show 1 min read List isEmpty() method in Java with Examples The isEmpty() method of the List interface in Java is used to check if a list is empty or not. It returns true if the list contains no elements otherwise it returns false if the list contains any element.Example:Java// Java Progrm to Implement // List isEmpty() Method import java.util.*; class Main 2 min read Java StringUtils.isNoneBlank() Method with Examples The StringUtils.isNoneBlank() is a method provided by the Apache Commons Lang library for string manipulation in Java. It is used to check if any of the given strings are not blank or empty. The StringUtils.isNoneBlank() function is part of the Apache Commons Lang library, which provides utility met 3 min read WeakHashMap isEmpty() Method in Java with Examples The java.util.WeakHashMap.isEmpty() method of HashMap class is used to check for the emptiness of the map. The method returns True if no key-value pair or mapping is present in the map else False. Syntax: Weak_Hash_Map.isEmpty() Parameters: The method does not take any parameters. Return Value: The 2 min read Optional get() method in Java with examples The get() method of java.util.Optional class in Java is used to get the value of this Optional instance. If there is no value present in this Optional instance, then this method throws NullPointerException. Syntax: public T get() Parameters: This method do not accept any parameter. Return value: Thi 2 min read Like