Java String isEmpty()24 Mar 2025 | 4 min read 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. The isEmpty() method in Java is a part of the String class and is used to check whether a string is empty or not. It returns a boolean value true if the string is empty (contains no characters), and false otherwise. An empty string is one that has a length of zero. This method is useful when you need to determine if a string contains any meaningful content before performing certain operations on it. For example, you might use isEmpty() to check if a user input field is filled out before processing the input. 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 ExampleExampleCompile and RunOutput: true false Java String isEmpty() Method Example 2ExampleCompile and RunOutput: String s1 is empty TpointTech Empty Vs. Null StringsEarlier in this section, 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. ExampleCompile and RunOutput: 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. ExampleCompile and RunOutput: The string is null. Let's understand in detail with the help of a Java example program. Example 1: ExampleCompile and RunOutput: str1 is empty: true str2 is empty: true str3 is empty: false str4 is empty: false str5 is empty: false str6 is empty: false str7 is empty: false str8 is empty: false 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. ExampleCompile and RunOutput: The string is blank. The string is not blank. It's important to note that isEmpty() is different from checking if a string is null. If a string variable is null, calling isEmpty() on it will result in a NullPointerException. To avoid this, it's a good practice to first check if the string is null before calling isEmpty(). In summary, the isEmpty() method in Java provides a simple and convenient way to check if a string contains any characters, helping to prevent errors when working with strings in your programs. Next TopicJava String join() |
Java The equals() function included in the String class is one of the many ways that Java can be used to work with strings. A quick and easy method to check if the contents of two strings match is to use this function. In this...
5 min read
Java String The Java String class method compares the two given strings on the basis of the content of the string irrespective of the case (lower and upper) of the string. It is just like the equals() method but doesn't check the case sensitivity....
3 min read
Java String.join() Method The Java String class join() method returns a string joined with a given delimiter. In the method, the delimiter is copied for each element. The join() method is included in the Java string since JDK 1.8. There are two types of join() methods...
5 min read
» « Java The Java String class startsWith() method checks if this string starts with the given prefix. It returns true if this string starts with the given prefix; else returns false. Signature The syntax or signature of startWith() method is given below. public boolean startsWith(String prefix) public boolean startsWith(String prefix,...
3 min read
» « Java The java string split() method splits this string against given regular expression and returns a char array. Internal implementation public String[] split(String regex, int limit) { /* fastpath if the regex is a ...
5 min read
Java The String.valueOf() method in Java is a multipurpose static method. Its major function lies in the conversion of types of data, such as primitive types and objects, into strings. The technique provides an efficient and convenient way to construct string objects from different sources. Java's...
7 min read
» « Java String manipulation is a fundamental aspect of programming, and Java offers a rich set of tools to accomplish various tasks related to strings. One of the handy methods provided by the String class is replaceAll() method. In this section, we will dive deep into...
4 min read
» « Java Java, being a versatile and widely-used programming language, provides a rich set of functionalities for handling strings. One powerful feature that developers often leverage is the substring method. The method allows us to extract a portion of a string, providing flexibility in manipulating and...
5 min read
» « Java String.indexOf() The Java String class indexOf() method returns the position of the first occurrence of the specified character or string in a specified string. Signature There are four overloaded indexOf() method in Java. The signature of indexOf() methods are given below: No. Method Description 1 int indexOf(int ch) It returns the index position...
5 min read
» « Java The Java String class format() method returns the formatted string by given locale, format and arguments. If we do not specify the locale in String.format() method, it uses the default locale by calling Locale.getDefault() method. The format() method of the Java Sting class is the same...
8 min read
We request you to subscribe our newsletter for upcoming updates.
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