Android TextUtils isEmpty vs String.isEmpty

前端 未结 5 930
南方客
南方客 2020-12-23 17:08

What is difference between TextUtils.isEmpty(string) and string.isEmpty?

Both do the same operation.

Is it advantageous to use

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 18:06

    Yes, TextUtils.isEmpty(string) is preferred.


    For string.isEmpty(), a null string value will throw a NullPointerException

    TextUtils will always return a boolean value.

    In code, the former simply calls the equivalent of the other, plus a null check.

    return string == null || string.length() == 0;
    

提交回复
热议问题