56

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

Both do the same operation.

Is it advantageous to use TextUtils.isEmpty(string)?

2

5 Answers 5

81

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;
7
  • @RuchirBaronia that means TextUtils.isEmpty(string) check also if the String object is null or not so if you try to use that String you didn't get NullPointerException Apr 3, 2016 at 17:33
  • @ahmedghanayem No, I was asking why cricket wrote String#isEmpty instead of String.isEmpty, but that link cleared that up for me. Apr 3, 2016 at 17:34
  • I realized I could simply use the variable instead of the class to avoid further confusion Apr 3, 2016 at 17:35
  • 1
    “always preferred” is a strong phrase. For references which should not be null, it’s actually preferred to use a method that will fail immediately, if the application is in an inconsistent state, rather than continuing with an assumption like “null is equivalent to an empty string” and fail at some unspecific later point.
    – Holger
    May 2, 2017 at 19:50
  • 1
    @cricket_007 Precisely, so if you did get a null string it's better to know about it immediately instead of masking the problem with TextUtils.isEmpty. You want TextUtils when working with strings which you do expect can be null e.g. because they are returned from some null-using API. Jun 16, 2017 at 11:46
11

In class TextUtils

public static boolean isEmpty(@Nullable CharSequence str) {
    if (str == null || str.length() == 0) {
        return true;
    } else {
        return false;
    }
}

checks if string length is zero and if string is null to avoid throwing NullPointerException

in class String

public boolean isEmpty() {
    return count == 0;
}

checks if string length is zero only, this may result in NullPointerException if you try to use that string and it is null.

1
  • Maybe I'm just stupid, but why are they not just doing: return if {str == null || str.length() == 0;}
    – jobbert
    Mar 22, 2018 at 15:21
5

Take a look at the doc

for the String#isEmpty they specify:

boolean
isEmpty() Returns true if, and only if, length() is 0.

and for the TextUtils.isEmpty the documentation explains:

public static boolean isEmpty (CharSequence str)

Returns true if the string is null or 0-length.

so the main difference is that using the TextUtils.isEmpty, you dont care or dont need to check if the string is null referenced or not,

in the other case yes.

0
5

TextUtils.isEmpty() is better in Android SDK because of inner null check, so you don't need to check string for null before checking its emptiness yourself.

But with Kotlin, you can use String?.isEmpty() and String?.isNotEmpty() instead of TextUtils.isEmpty() and !TextUtils.isEmpty(), it will be more reader friendly

So I think it is preferred to use String?.isEmpty() in Kotlin and TextUtils.isEmpty() in Android Java SDK

1
  • 2
    Using TextUtils.isEmpty() return false even if it is empty while running tests. It needs static mocking. If you are using Kotlin use isNullOrBlank() or isNullOrEmpty() methods instead. You don't need any mocking for tests.
    – Rajkiran
    Mar 14, 2019 at 5:40
-2
String?.isNullOrEmpty

might be what you are looking for

1
  • 3
    This is a java question.
    – Sabrina
    Sep 29, 2019 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.