The isEmpty()
method is a method of the String
class in Java that can be used to check if a string is empty. It returns true
if the string has a length of 0, and false
if it has a non-zero length.
Here is an example of how to use the isEmpty()
method:
String s1 = ""; String s2 = "hello"; System.out.println(s1.isEmpty()); // Outputs true System.out.println(s2.isEmpty()); // Outputs false
In this example, the first call to the isEmpty()
method returns true
because the string s1
is empty. The second call to the isEmpty()
method returns false
because the string s2
is not empty.
You can also use the isEmpty()
method to check if a StringBuilder
object is empty, like this:
StringBuilder sb = new StringBuilder(); System.out.println(sb.length() == 0); // Outputs true sb.append("hello"); System.out.println(sb.length() == 0); // Outputs false
In this example, the first call to the length()
method returns 0 because the StringBuilder
object sb
is empty. The second call to the length()
method returns a non-zero value because the StringBuilder
object sb
has been modified to contain the string "hello".
For more information on the isEmpty()
method and other methods provided by the String
and StringBuilder
classes in Java, you can refer to the Java documentation or other online resources.