In Java, dealing with strings is a common task, and it's essential to handle null or empty strings to prevent potential errors in your code. This blog post will guide you through various methods to check if a string is null or empty in Java.
Before we dive into the methods, let's understand the difference between null and empty strings:Empty String: An empty string is a string object that exists in memory, but it contains no characters. It has a length of 0.
1. Using equals() Method
The equals() method can be used to check if a string is null-safe and prevents a NullPointerException. It returns true if the string is not null and contains characters.
Example:
public class CheckStringExample {
public static void main(String[] args) {
String str1 = null;
String str2 = "";
String str3 = "Hello";
System.out.println(str1 == null || str1.equals("")); // Output: false
System.out.println(str2 == null || str2.equals("")); // Output: true
System.out.println(str3 == null || str3.equals("")); // Output: false
}
}
Output:
true
true
false
2. Using isEmpty() Method
The isEmpty() method is a built-in method in Java that can be used to check if a string is empty (contains no characters). It returns true if the length of the string is 0.
Example:
public class CheckStringExample {
public static void main(String[] args) {
String str1 = null;
String str2 = "";
String str3 = "Hello";
System.out.println(str1 != null && str1.isEmpty()); // Output: false
System.out.println(str2 != null && str2.isEmpty()); // Output: true
System.out.println(str3 != null && str3.isEmpty()); // Output: false
}
}
Output:
false
true
false
3. Using isBlank() Method (Java 11+)
The isBlank() method is introduced in Java 11, and it not only checks for empty strings but also considers strings containing only white spaces as blank.
Example:
public class CheckStringExample {
public static void main(String[] args) {
String str1 = null;
String str2 = "";
String str3 = " "; // Contains only white spaces
System.out.println(str1 != null && str1.isBlank()); // Output: false
System.out.println(str2 != null && str2.isBlank()); // Output: true
System.out.println(str3 != null && str3.isBlank()); // Output: true
}
}
Output:
false
true
true
4. Using Objects Class (Java 7+)
In Java 7 and later versions, the Objects class provides a utility method isNull() that can be used to check for null values.
Example:
import java.util.Objects;
public class CheckStringExample {
public static void main(String[] args) {
String str1 = null;
String str2 = "";
String str3 = "Hello";
System.out.println(Objects.isNull(str1)); // Output: true
System.out.println(Objects.isNull(str2)); // Output: false
System.out.println(Objects.isNull(str3)); // Output: false
}
}
Output:
true
false
false
5. Write Your Own Utility Methods
- isEmptyOrNull(final CharSequence cs) - Checks if a CharSequence is empty ("") or null.
- isNotEmptyOrNull(final CharSequence cs) - Checks if a CharSequence is not empty ("") and not null.
1. isEmptyOrNull(final CharSequence cs)
public static boolean isEmptyOrNull(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
2. isNotEmptyOrNull(final CharSequence cs)
public static boolean isEmptyOrNull(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
public static boolean isNotEmptyOrNull(final CharSequence cs) {
return !isEmptyOrNull(cs);
}
package net.javaguides.lang;
public class StringNullOrEmptyExample {
public static void main(String[] args) {
System.out.println(isEmptyOrNull(null));
System.out.println(isEmptyOrNull(""));
System.out.println(isEmptyOrNull(" "));
System.out.println(isEmptyOrNull("bob"));
System.out.println(isEmptyOrNull(" bob "));
System.out.println(isNotEmptyOrNull(null));
System.out.println(isNotEmptyOrNull(""));
System.out.println(isNotEmptyOrNull(" "));
System.out.println(isNotEmptyOrNull("bob"));
System.out.println(isNotEmptyOrNull(" bob "));
}
public static boolean isEmptyOrNull(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
public static boolean isNotEmptyOrNull(final CharSequence cs) {
return !isEmptyOrNull(cs);
}
}
Output:
true
true
false
false
false
false
false
true
true
true
Conclusion
Checking whether a string is null or empty is a crucial step in Java programming, especially when handling user inputs or external data. You can use any of the methods discussed in this blog post to perform null-safe and empty-safe string checks based on your specific requirements. I would suggest creating the Utility methods for null checks in your Java projects.
Comments
Post a Comment
Leave Comment