Why strings are immutable

Viktor Karpov
1 min readFeb 3, 2016

--

String values are immutable in many languages, for instance golang and javascript have ones.

Do we really create a ton of copies? Nope. Two copies of a string share the same data under the hood.

Immutability means that it’s safe to share underlying data.

What if we change a string directly?

JavaScript stays in silence. Golang throws a compile error warning about that probably you’re doing something you don’t want to.

If we could change the string in such way then all strings with the same value have been changed!

--

--