A string is a reference data type which is used to store text. Most of the developers consider
string as a value type because of its behavior like value type. It is a small but very imperative concept to understand, why
string is behaving like value type. To achieve this, there is a need to understand the actual problem, reason of this situation.
Before coming to the reason, we need to understand value type and reference type.
A data type that stores its content in the stack (a special region of memory). Value type holds the value and if you are going to assign it to another variable, the value will be copied directly and both the variables will work independently. Let’s understand with an example.
A data type that stores its content in the heap (a special region of memory). In reference type variable, it holds the reference (address) of the object instead of the value assigned to that object. Whenever you assign one object to another, it will create a second copy of reference which will refer to the same location. So that in case of changing any one will reflect to the other as both are referring to the same location. Let’s understand with an example.
Square sqr1 = new square ();
Square sqr2 = new square ();
sqr1.length = 10;
sqr2.length = 15;
After having a basic understanding pertaining to value and reference type, now we can easily presume that how
string should behave as it is a reference type but what we see is, it’s behaving like a value type. The answer is because
string is immutable. This one line statement has some depth. So it is imperative to understand or get a deeper dive into mutable and immutable.
In Immutable (which means Unchangeable) objects, the state of the object cannot change once it has been created. On every modification, it will not make changes in the same location but will create a new instance in memory.
In mutable (which means changeable) objects, the state of the object can be changed after its creation. On any changes, it will change on the same location in the memory.
After having an understanding about value, reference type along with Mutable and immutable, now it’s time to find why
string behaves like a value type as it is a reference type. The reason is that whenever we create a
string and pass that
string’s reference to another
string, it copies the reference (memory address) of the referred
string so that both
strings will refer to the same location but when we make changes in either
string, it creates a new location in memory and updates the text in that location. So the other (second)
string will keep referring to the previously location. So after any change in the
string, both variables will refer to separate memory locations. Let’s do it with an example.
strings with the names
Both will point to separate locations in memory.
strings are referring to the same location in memory. Now make changes in
str1. Here, the immutable concept will apply (on every change, create new location in memory).
Now we can understand that
str1 now will locate to new memory (3) and
str2 will still be located on the previous memory location (1) where nothing is changed. That’s how, on any modification,
string does not get updated.