- A
String
is immutable (ie, the text can’t change). It also doesn’t have any spans associated with it. (Spans are ranges over the text that include styling information like color, highlighting, italics, links, etc.) So you can use aString
when your text doesn’t need to be changed and doesn’t need any styling. - A
StringBuilder
has mutable text, so you can modify it without creating a new object. However, it doesn’t have any span information. It is just plain text. So use aStringBuilder
when you need to change the text, but you don’t care about styling it. - A
SpannedString
has immutable text (like aString
) and immutable span information. It is a concrete implementation of the requirements defined by theSpanned
interface. Use aSpannedString
when your text has style but you don’t need to change either the text or the style after it is created.
Note: There is no such thing as a SpannedStringBuilder
because if the text changed then the span information would also very likely have to change.
- A
SpannableString
has immutable text, but its span information is mutable. It is a concrete implementation of the requirements defined by theSpannable
interface. Use aSpannableString
when your text doesn’t need to be changed but the styling does. - A
SpannableStringBuilder
has both mutable text and span information. It is a concrete implementation of the requirements defined by theSpannable
andEditable
interfaces (among others). Use aSpannableStringBuilder
when you will need to update the text and its style. - A
CharSequence
is an interface and not a concrete class. That means it just defines a list of rules to follow for any class that implements it. And all of the classes mentioned above implement it. So you can use aCharSequence
when you want to generalize the type of object that you have for maximum flexibility. You can always downcast it to aString
orSpannableStringBuilder
or whatever later if you need to.