As a matter of course, org.apache.commons.lang.StringUtils.isEmpty () `` `is used for null checking of strings, but in fact,
java.lang.String.isEmpty () There is also `. So why use ```org.apache.commons.lang.StringUtils.isEmpty ()
`to include an external jar?
org.apache.commons.lang.StringUtils
.Returns true if null or empty string.
Returns true only if length () is 0.
Here is the source of actually using the isEmpty () method for null and empty string: arrow_down:
import org.apache.commons.lang.StringUtils;
public class VsIsEmpty {
public static void main(String[] args) {
String str1 = null;
String str2 = "";
System.out.println("-------------------------");
// System.out.println(str1.isEmpty()); //NullPointerException occurs here.
System.out.println(StringUtils.isEmpty(str1));
System.out.println("-------------------------");
System.out.println(str2.isEmpty());
System.out.println(StringUtils.isEmpty(str2));
}
}
Click here for the execution result: arrow_down:
-------------------------
true
-------------------------
true
true
String.isEmpty () returns NullPointerExceptin just because it says "when length () is 0". Therefore, it is commented out in the above source code.
String.isEmpty () was a "empty" child: exclamation: Since null and sky are different things, it seems to be true when asked, but after all empty means that I want both null and sky to be returned as true.
Recommended Posts