![]() |
VOOZH | about |
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.
true false
Table of Content
boolean isEmpty()
Return Value:
To ensure that the user input is not empty before processing it, we can use isEmpty() method of String class.
Example:
Enter a username: No input provided.
We can use isEmpty() method to validate multiple strings.
Example:
Is string at index 0 empty? false Is string at index 1 empty? true Is string at index 2 empty? false
To handle different scenarios, combine isEmpty() with conditional checks.
The message is empty.
Explanation:
The above example checks if the m string is empty using the isEmpty() method and prints the message.