0

I’m very new to coding and want to know what an immutable string is. I’ve read that this means that a string object can’t be changed once it’s created.

String = ‘hello’

String = ‘bye’

Would the second expression not work because that variable already exists? Also, if I wanted to change the first string, couldn’t I just edit that one directly?

Frank
  • 1
  • I think some of your confusion is equating _variables_ with _values_. The two are actually very different things in most languages. Typically variables _reference_ some value, and can be freely re-assigned to reference a different value. Mutability and immutability come up when you have multiple variables that reference _the same value_. Immutability means that you can't modify a value in place. This is useful, because it means that different variables won't "step on each others toes": actions taken on a value from one variable won't affect the values visible through other variables. – Brian61354270 Aug 24 '23 at 13:45

0 Answers0