I Spy Code - Java

Check if string is empty or null

Question:

Write a java program that checks if a string is empty or null:

Solution:

Here is a java example that verifies if a string is empty or null:

Source: (Example.java)

import org.apache.commons.lang.StringUtils;
 
public class Example {
 
    public static void main(String[] args) {
        String str1 = null;
        String str2 = "";
        String str3 = "hello";
 
        System.out.println(StringUtils.isEmpty(str1));
        System.out.println(StringUtils.isEmpty(str2));
        System.out.println(StringUtils.isEmpty(str3));
 
    }
}
 

Output:

$ java Example
true
true
false

References:

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isEmpty-java.lang.CharSequence-

Questions answered by this page:

Checking if a string is empty or null in Java
Best way to verify string is empty or null using java