Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How string Behaves Like Value Type as it is reference Type

0.00/5 (No votes)
6 Sep 2017 2  
String behaves like value type as it is reference type

Introduction

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.

Value 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.

Int a=50;    //declare and initialize variable a.
Int b=50;    //declare and initialize variable b.
b=a;         //Assign variable a to b. Here only the value of variable will be copied to b.
             //Now changes in variable a or b will not reflect to each other.
b=30;        //Only value of b variable will change and variable a will hold 50 as its previous value.

Reference Type

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 (); //declaration of an object (reference type)
Square sqr2 = new square (); //declaration of another array (reference type)
sqr1.length = 10;            //assign value to length property of square class (sqr1)
sqr2.length = 15;            //assign value to length property of square class (sqr2)
sqr2= sqr1;                  //assign sqr1 to sqr2
sqr2.length=30;              //Now in changing in sqr1 will reflect to sqr2.

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.

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.

Mutable

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.

Create two strings with the names str1 and str2.

string  str1;
string str2;

Both will point to separate locations in memory.

Now assign str1 to str2.

str2=str1;

Both 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here