Android - What is the difference between String and Editable?

Asked

Viewed 171 times

7

When using the Edittext view, I saw that it returns an Editable data type and not a String. And it has different methods like getEditableText().

What is the main difference between String and this other type of data Editable? Could not a Edittext return a simple String?

Explanations Complete are greatly appreciated

  • 2

    According to the documentation, the difference is mutability, that is, the ability to change the data. Strings are immutable, when you concatenate, a new string is created with the old value and the new together, so I understand, this Editable does not need this.

  • I hadn’t really noticed it, just as I commented in the ramaral response. When editing a String, you need to create a new one. That really makes sense. Thank you Articuno!

1 answer

8


There are two main differences

  • Mutability

    Whenever any operation is applied to a String a new one is created, the same does not happen in Editable where the operation is applied in the current instance.

  • Possibility to add Markup Objects(markup/style objects).

    An Editable can be added Markup Objects to change the appearance/style of the text, as underlined(Underlinespan), crossed out(Strikethroughspan) and Bold(Stylespan), amid others.

  • It makes sense. Actually when manipulating a defined string it is necessary to create another string. I wonder if in this case, it would not be more convenient to return a Stringbuilder not? Than another type like Editable, although by its explanation, it is useful to add style to the text. Anyway, thank you so much for the answer! It was very helpful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.