String is immutable

Someone asked me an interesting question. String is immutable. So how can we reassign a value to a string variable?

String is immutable

Example of string immutable

The computer allocates memory to store John in firstname. When Matt is assigned to the same string, the computer finds a different space in memory to store Matt. Thereafter, space where John was stored is released. This process is why string has immutability (one cannot change John to Matt), but string’s value can be changed.