Guava Strings.isNullOrEmpty() method in Java
In this post, we will explore how to use the Guava Strings.isNullOrEmpty() method to check for null or empty strings in a concise and elegant way.
As a Java developer, you probably know that checking for null or empty strings is a common and tedious task. You may have encountered situations where you need to validate the input, output, or intermediate results of your code, and handle them accordingly. For example, you may want to throw an exception, return a default value, or skip some logic if a string is null or empty.
1. Usage of Strings.isNullOrEmpty() method
A convenient way to check whether a given string is null or empty is using the Guava Strings.isNullOrEmpty() method. The syntax of this method is:
|
1 |
public static boolean isNullOrEmpty(@Nullable String string) |
This method returns true if the string is null or has zero length; otherwise it returns false. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.base.Strings; class Main { public static void main(String[] args) { String s1 = null; String s2 = ""; String s3 = " "; String s4 = "Hello"; System.out.println(Strings.isNullOrEmpty(s1)); // returns true System.out.println(Strings.isNullOrEmpty(s2)); // returns true System.out.println(Strings.isNullOrEmpty(s3)); // returns false System.out.println(Strings.isNullOrEmpty(s4)); // returns false } } |
The Guava Strings.isNullOrEmpty() method can be used to simplify and improve the readability of your code when checking for null or empty strings. It can also be used in conjunction with other Guava utilities, such as Preconditions, Optional, etc.
2. Comparing with other ways in Java
There are other ways to achieve the same functionality as the Strings.isNullOrEmpty() method in Java, such as:
- Using the
Stringclass and its methods, such asequals(),length(), orisEmpty(). However, these methods are verbose and repetitive to write and read. They also require multiple conditions or null-safe operators to handle bothnulland empty strings. They may also not be consistent with the semantics of your code, such as treating whitespace-only strings as empty or not. - Using third-party libraries or frameworks that provide utility methods for checking
nullor empty strings, such as Apache Commons Lang, Spring Framework, or Android SDK. However, these libraries or frameworks require an additional dependency, which may not be desirable for some projects or environments. They may also not provide any additional benefits over theStringclass methods, such as performance or readability.
3. Conclusion
In this post, we have learned how to use the Guava Strings.isNullOrEmpty() method to check for null or empty strings in Java. We have seen how the Guava Strings class can help us overcome some of the limitations of the String class or other libraries. We have also explored some of the ways to use the Guava Strings class with other Guava utilities.
For more information about this method, you can check out the Guava official documentation or its GitHub repository.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)