Click here to Skip to main content
13,143,774 members (31,944 online)
Click here to Skip to main content
Add your own
alternative version

Stats

5.3K views
17 downloads
7 bookmarked
Posted 5 Sep 2017

How string Behaves Like Value Type as it is reference Type

, 6 Sep 2017
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

israrali
Pakistan Pakistan
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
Questionwhy? Pin
Philip Liebscher11-Sep-17 16:23
memberPhilip Liebscher11-Sep-17 16:23 
PraiseWell explained Pin
aeastham7-Sep-17 2:05
memberaeastham7-Sep-17 2:05 
QuestionGood explanation on how... Pin
lmoelleb6-Sep-17 3:45
memberlmoelleb6-Sep-17 3:45 
AnswerRe: Good explanation on how... Pin
israrali6-Sep-17 4:06
memberisrarali6-Sep-17 4:06 
PraiseGood Pin
Member 132317455-Sep-17 9:35
memberMember 132317455-Sep-17 9:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 6 Sep 2017
Article Copyright 2017 by israrali
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid