Before we get started between the difference of value type variables and reference type variables, we need to understand the concept of stack and heap. So, let’s get started with stack and heap.
Stack VS Heap
We’re actually working in the machine environment and we already know that the way we treat with a program is different and it executes in a different way under the hood. Normally, we assign the single value in the container and since the value is single, so we call such kind of container a static memory which holds only 1 value.
- int x = 123;
This value stores on the top of the stack.
We already know that these values are actually meaningful for us but computer understands the values in numbers and we know that the ASCII Code of ‘A’ is 65. So let’s suppose,
- char c = ‘A’;
So computer understands ‘A’ itself as a number 65 under the hood. And as we can see ‘A’ is just a single value, 123 is also a single value. So, we call these containers as static containers and these variables are stored into the stack (LIFO) and they are not complex.
But the datatypes which hold more than 1 values, these datatypes are the complex datatypes. We know the architecture of Stack and heap.

In Stack, variables are places one by one on the top of each other in memory. But heap is actually the dynamic memory which we use according to our need.
Let’s discuss the comparison between string and char,
As we know that string can be any length but char just holds the single character. So if,
- char c1 = ‘a’;
- char c2 = ‘b’
- char c3 = c1 + c2;
Then we’ll see the error because char can hold just single value (ASCII number of a single value) and here, we’re using the syntax of the concatenation of string. But as the string can be any length. So, it works
- string str1 = “a”;
- string str2 = “b”;
- string str3 = str1 + str2;
Here string can hold large values so these strings join with each other and here it works.
Why string and class are reference types?
string, object, class are actually store into the heap because they are dynamic. Let’s discuss them.
- string name = “Muhammad”;
Look we’ve already discussed that computer understands the alphabets into the numbers. And here we have 8 letters which have different ASCII numbers. So it stores the data into the heap and we’ll just pass the reference to the first letter in the stack. See the images below and you’ll get the idea what is happening under the hood.

And here these items are actual characters and they store into the memory into the ASCII numbers form.

This is how string works. As the program executes into the stack, the stack block points to the first character of the string because it has the reference to the first character’s memory reference. And then this first character has the address of the next character reference block which has the value of the 2nd character.



Hadshana KamalanathanPosted Aug 16, 2018, 1:03 AM
Thanks for sharing
Usama ShahidPosted Apr 4, 2018, 4:48 AM
Thank You So Much Guys For Your Support. You People Are Motivating Me To Keep Sharing My Knowledge :)
Satish Kumar VadlavalliPosted Apr 4, 2018, 2:01 AM
Good, keep it up :)
Naresh SinghalPosted Apr 4, 2018, 12:19 AM
Very good explained Usamabhai
Rafnas T PPosted Apr 3, 2018, 2:07 AM
Nice explanation, keep sharing..