[Java] Why use StringUtils.isEmpty () instead of String.isEmpty ()

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?

Difference between two isEmpty ()

StringUtils isEmpty ()

Returns true if null or empty string.

String isEmpty ()

Returns true only if length () is 0.

Comparison of execution results of isEmpty () of String and StringUtils

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.

Conclusion

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

[Java] Why use StringUtils.isEmpty () instead of String.isEmpty ()
Why use setters/getters instead of public/private in Java
[Swift] Why FlowLayout should use Delegate instead of instance
Use enum instead of int constant
Item 36: Use EnumSet instead of bit fields
[Java] [Maven3] Summary of how to use Maven3
Use jenv to enable multiple versions of Java
[Java] Use of final in local variable declaration
[Java] Use Collectors.collectingAndThen
[Java] Overview of Java
[Java] How to use compareTo method of Date class
Use Java lambda expressions outside of the Stream API
Browse class objects in Kotlin (instead of Java class name.class)
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Use of Abstract Class and Interface properly in Java
Java: Use Stream to sort the contents of the collection
[Null safety] Kotlin Java comparison memo Correct use of Null safety
Expired collection of java
Use of Date class
Predicted Features of Java
[Java] Significance of serialVersionUID
NIO.2 review of java
Review of java Shilber
Why we use Docker
java --Unification of comments
Use OpenCV in Java
History of Java annotation
java (merits of polymorphism)
NIO review of java
Use PreparedStatement in Java
[Java] Three features of Java
Summary of Java support 2018
How to use trained model of tensorflow2.0 with Kotlin / Java