Check if a String is Empty in Java - Using String.isEmpty()

Check if a String is Empty in Java: What does this mean? Nothing, check whether the length of the string is 0 or not. That's it and nothing more than that. Looking simple? Yes it looks, the isEmpty() method makes it much more easier than you do in other programming languages like C/C++, like just determining the length of the char array, and checking if it is 0 or not. If it is, then it is empty if it is not then it is non-empty. Here is how-to.


class CheckStringisEmpty
{
public static void main(String args[])
{
// Create empty string
String st="";
// Check if it is empty
boolean empty=st.isEmpty();
// Print true/false
System.out.println("Is st empty? "+empty);
// Fill some text
st="Not Empty";
// Check if it is empty
empty=st.isEmpty();
// Print true/false
System.out.println("Is st empty? "+empty);
}
}

st.isEmpty(): This method belongs to java.lang.String class, this method identifies, which string object is to be checked, and then checks whether it is empty or not by simply, determining the length of the string, i.e. if the length of the string is 0 then, true is returned else false is returned.

Here is an example, to determine the length of the string.

-----------------------------------
Output
-----------------------------------

Is st empty? true
Is st empty? false