5

In Kotlin (Android project), which is preferable to use ?

isNullOrEmpty or TextUtils.isEmpty and why

I believe they do the exact thing and are "null" safe, right ?

1
  • They both check for null and a length of 0, so they just do the same... I would then stick to Kotlin and use CharSequence.isNullOrEmpty
    – deHaar
    Commented Dec 13, 2019 at 11:18

7 Answers 7

8

Based off of my question here

If your project will be ported to other frameworks, making use of isNullOrEmpty will probably result in a safer experience, because it's based on Kotlin (specifically package kotlin.text) and not on package android.text;

Why is this safer ?

Well, the implementation of isNullOrEmpty might change depending on the platform but it will be available to use and is part of Kotlin, compared to TextUtils.isEmpty being only for android.

4
  • What other platform other than Android was OP referring to? Commented Mar 15, 2020 at 18:09
  • @IgorGanapolsky with Kotlin Native being a thing (no expert on this) i believe that you can write Kotlin code which could be converted to ios or to android, if that answers your comment ? Commented Mar 15, 2020 at 18:11
  • Interesting, haven't heard of anybody developing Kotlin Native in production at this point. Commented Mar 15, 2020 at 18:14
  • 1
    me neither @IgorGanapolsky, this question was just related to something i found myself while coding normal android :) Commented Mar 15, 2020 at 18:24
3

I would say isNullOrEmpty is slightly preferable, because it's a language's feature. TextUtils.isEmpty is a library function, external to Kotlin.

1

For string.isEmpty(), a null string value will throw a NullPointerException Incase of isNullOrEmpty it will check first value is null or not and it will proceed for the empty check. isNullOrEmpty is preferable

2
  • 1
    What about TextUtils.isEmpty, which was mentioned in the question? The question did not mention String.isEmpty
    – deHaar
    Commented Dec 13, 2019 at 11:19
  • Both will be the same, The only difference is we are taking the advantage of kotlin extensions here in kotlin Commented Dec 13, 2019 at 11:24
1

The code for TextUtils.isEmpty:

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

The code for isNullOrEmpty:

public inline fun CharSequence?.isNullOrEmpty(): Boolean {
    contract {
        returns(false) implies (this@isNullOrEmpty != null)
    }

    return this == null || this.length == 0
}

Witch is basically the same, the inline function also has a contract that will tell the compiler to return false if the value is not null, not sure what the actual usage is, maybe it's an optimization.

1

Yes , the only difference is NullPointerException.

In isNullOrEmpty first it will check string is null or not .if it is not null then it will check it is check string is empty or not . If any of the condition will true then method will return true.

In TextUtils.isEmpty it check string is empty or not .If string is null then it will throw NullPointerException and your app will crash .

1
  • That is not true. public static boolean isEmpty(@Nullable CharSequence str) { return str == null || str.length() == 0; } Commented Nov 10, 2023 at 20:14
1

TextUtils.isEmpty is basically the same thing with isNullOrEmpty(). Yet, there are differences in terms of availability and compilation:

1) TextUtils.isEmpty is available from the android.text package, available with Android SDK, and isNullOrEmpty() requires Koltin dependency (kotlin.text package).

2) Another difference is that isNullOrEmpty() is an inline extension function and it is compiled differently, perhaps with a slight performance boost.

Apart from these, there is another useful inline function isNullOrBlank(). It also returns true if the String contains only white spaces.

0

In Kotlin isNullOrEmpty is preferable. TextUtils.isEmpty is specific to Java. Both returns boolean if string is empty or null.

1
  • Are there exceptions thrown? Commented Mar 15, 2020 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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