When you do:
textview.text = pString
What actually happens?
TextView.text
is a property backed by a CharSequence
. When you assign textview.text = pString
, Android calls TextView.setText(CharSequence)
internally.
pString
is a String
, which implements CharSequence
. So setText(CharSequence)
accepts it directly.
Internally, the TextView
stores the CharSequence
reference you pass in, but it does not promise to keep a direct reference to the same String
object forever — it wraps it in an internal Spannable
or Editable
if needed, depending on features like styling, input, etc.
Does it copy the string?
For immutable plain String
s, Android does not immediately clone the character data. It stores the String
reference (or wraps it in a SpannedString
or SpannableString
if needed).
If you later modify the text (e.g., if the TextView
is editable, or you apply spans), it may create a mutable copy internally (Editable
) — but your original String
(mystring
) is immutable, so it can’t be changed.
In short:
textview.text = pString
does not copy the String
characters immediately — it just passes the reference to TextView
’s internal text storage.
The String
itself is immutable, so mystring
stays the same.
If the TextView
needs to change the text (like user input in an EditText
), it works on a mutable Editable
copy internally.
Therefore: No new copy of the string’s character data is created at assignment. Just the reference is stored/wrapped as needed.