Scientech Easy Memory allocation of Primitive & Non-primitive Data Type | Scientech Easy

Saturday, June 30, 2018

Memory allocation of Primitive & Non-primitive Data Type

In this tutorial, we will learn Memory allocation of Primitive and Non-primitive Data type. I will try to understand you this concept in easy ways so that you do not get any problem to understand this topic. This topic is very important to understand the memory allocation.

Memory allocation of Primitive & Non-primitive


All data type for primitive type variables is stored on the stack.
For reference types, the stack holds a pointer to the object on the heap.
When setting a reference type variable equal to another reference type variable, a copy of only the pointer is made.
Certain object types cannot be manipulated on the heap.
Let's understand all these points by taking one simple example. see below image.


To understand all the above points, we have taken three blocks. The first block represents code, the second block represents the data structure stack memory and the third block is heap memory.

In the first line of code, we declared a primitive variable int a=3;. Since 'a' is primitive type so all data stores on the stack.
Next, In the second line of code, we declared a variable type int b=a;. Since 'b' is a primitive type and 'a' is also primitive type. Therefore, JVM just copies the data from a to b.

In the third line, If you modify the value of variable b equal to 100 then JVM will change the entry 100 on the stack as b equal to 100(b=100). see the image.
Let's take some reference type variable i.e Non-primitive type. 'c' is a reference type variable and 'c' is declared as an integer array. where integer array is an object. So JVM will set c on the stack and point the pointer to an object on the heap. This object is an array of size four with values 1, 2, 3, 4 in the index as shown in the image. The index starts from 0, 1, 2, 3. Remember, only data stored in c on the stack is a pointer.

In the fifth line, a variable d is also declared as integer array type and set equal to c. So in this case, JVM will copy the data from the stack to d. Now c and d both are pointing to the same object on the heap.

In the six line of code, an integer array e is declared which is pointing a new object on the heap.
Again, an integer array f is created and pointing to the new object on the heap. In the above image, you can notice that the values are the same but both e and f are pointing to the different objects on the heap. 
In the last line of code, a variable g is declared as a string which is the Non-primitive data type and it is pointing to the "hello" on the heap.

Thus, you have seen that all primitive data types are stored on the stack and in the reference type, stack holds a pointer to the object on the heap.

Final words:
I hope this tutorial will help you to understand the Memory allocation of Primitive and Non-primitive data type. If you study one to two times this tutorial then you can easily understand all important points.
                                                                                         
                                                         "Knowledge is power".

Popular