# Why are strings immutable (historical records request)

**URL:** <https://discourse.julialang.org/t/why-are-strings-immutable-historical-records-request/92115>\
**Category:** Community\
**Tags:** strings\
**Created:** [December 25, 2022, 9:50pm UTC](https://discourse.julialang.org/t/why-are-strings-immutable-historical-records-request/92115 "2022-12-25T21:50:27Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 12, 2023, 5:00pm UTC](https://discourse.julialang.org/t/why-are-strings-immutable-historical-records-request/92115/5 "2023-01-12T17:00:01Z")

</div>

I can’t find the origin in emails, but this was my decision, effected by not providing any mutating APIs to strings. [Here’s](https://groups.google.com/g/julia-dev/c/d90FrbXRObY/m/zabmcZ8lgW0J) an older more direct explanation of why from 2012:

> There’s an important psychological distinction between things we think of as mutable versus immutable: values that are _defined_ by their values should be immutable, whereas values that _contain_ other values but have an independent identity can be mutable. Numbers are a prototypical example of being defined by their values — if you change the imaginary part of a complex number, you have a different number, not the same number altered (it sounds weird to even talk about “altering” a number). Arrays are the prototypical containers: change the contents of an array all you want and it still has an independent identity (psychologically).
> 
> The reason I believe strings are so dangerous as mutable values in high-level languages is that we think of them as being defined by their value: the string “foo” and “fox” are simply different values, regardless of whether the former was altered into the latter using the same memory. However, as a legacy from the implementation and the fact that in C they are just arrays of bytes, a low-level view of them is as containers. In C that’s natural and fairly unsurprising because you’re used to thinking about the actual memory buffer that represents a string. In a higher level language, it’s really, really surprising when a string changes value because you passed it to some other function. Some of the nastiest bugs I’ve ever had to find were due to this.

---

_[View the full topic](https://discourse.julialang.org/t/why-are-strings-immutable-historical-records-request/92115)._
