Java String isEmpty()The Java String class isEmpty() method checks if the input string is empty or not. Note that here empty means the number of characters contained in a string is zero. SignatureThe signature or syntax of string isEmpty() method is given below: Returnstrue if length is 0 otherwise false. Since1.6 Internal implementationJava String isEmpty() method exampleFileName: StringIsEmptyExample.java Test it NowOutput: true false Java String isEmpty() Method Example 2FileName: StringIsEmptyExample2.java Output: String s1 is empty Javatpoint Empty Vs. Null StringsEarlier in this tutorial, we have discussed that the empty strings contain zero characters. However, the same is true for a null string too. A null string is a string that has no value. The isEmpty() method is not fit for checking the null strings. The following example shows the same. FileName: StringIsEmptyExample3.java Output: Exception in thread "main" java.lang.NullPointerException at StringIsEmptyExample3.main(StringIsEmptyExample3.java:7) Here, we can use the == operator to check for the null strings. FileName: StringIsEmptyExample4.java Output: The string is null. Blank StringsBlank strings are those strings that contain only white spaces. The isEmpty() method comes in very handy to check for the blank strings. Consider the following example. FileName: StringIsEmptyExample5.java Output: The string is blank. The string is not blank. Next TopicJava String join() |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India