2

While trying to comprehend this answer, I failed to understand the difference between StringBuilder and Editable. This image (minus the hand-drawn red circles) comes from that answer.

enter image description here

Both of them have methods to append, delete, insert, and replace text. I see that StringBuilder is an actual class while Editable is an interface. But I can get an Editable back from a TextView with textView.getEditableText(), which seems strange if it is an interface. What is the concrete implementation behind the scenes? I also see that Editable can have spans while a StringBuilder doesn't.

I think I am close to understanding the difference, but I could use a little more explanation. I was surprised that I couldn't find where this question had already been asked, so I am asking it now.

Again, specifically:

  • What is the difference between StringBuilder and Editable?
  • When should I use one as opposed to the other?
Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 1
    Editable can hold markups such html. StringBuilder holds only plain text. What do you mean by *which seems strange if it is an interface* ? – Blackbelt Jan 30 '17 at 10:18
  • Is the object that I am getting back from `TextView` actually a `SpannableStringBuilder`? And if so, why do I get an incompatible types error if I write `SpannableStringBuilder myString = textView.getEditableText();`? @Blackbelt – Suragch Jan 30 '17 at 10:22
  • My question may be evolving into: What is the difference between `SpannableStringBuilder` and `Editable`? – Suragch Jan 30 '17 at 10:24
  • a `SpannableStringBuilder` is an `Editable`, but an `Editable` is not a `SpannableStringBuilder`. The former is something concrete the latter is an abstraction. You can't assign `getEditableText` to a `SpannableStringBuilder` unless you down cast it. *Is the object that I am getting back from TextView actually a SpannableStringBuilder*, it might be and it might be not. – Blackbelt Jan 30 '17 at 10:30
  • @Blackbelt. OK, I think that all makes sense now. Feel free to make your comments an answer. – Suragch Jan 30 '17 at 10:36

1 Answers1

1

What is the difference between StringBuilder and Editable?

Editable can hold markups such html. StringBuilder holds only plain text.

But I can get an Editable back from a TextView with textView.getEditableText(), which seems strange if it is an interface. What is the concrete implementation behind the scenes?

It is not strange. It returns an Editable, the concrete implementation depends on how you set the text.

is the object that I am getting back from TextView actually a SpannableStringBuilder? And if so, why do I get an incompatible types error if I write SpannableStringBuilder myString = textView.getEditableText();

It might be or it might be not. What you know is that is an Editable. A SpannableStringBuilder is an Editable, but an Editable is not a SpannableStringBuilder, that's why you can't assign the return value of getEditableText to a SpannableStringBuilder unless you down cast it. I

Blackbelt
  • 156,034
  • 29
  • 297
  • 305