?? Understanding Memory Allocation: Primitive vs Non-Primitive Data Types

?? Understanding Memory Allocation: Primitive vs Non-Primitive Data Types

Ever wondered how different data types impact memory usage in your applications? Understanding memory allocation for primitive and non-primitive data types is crucial for optimizing performance and efficiency. Let's dive into how it works!

? In Java, all data type for primitive type variables is stored on the stack.

? For reference data 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 as shown in the below figure.



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.

1. In the first line of code, we declared a primitive type variable int a = 3;. Since ‘a’ is a primitive type variable whose data type is int. So, all data will be allocated on the stack memory as shown in the second block of the above figure.

2. In the second line of code, we declared a variable type int b = a;. Since ‘b’ is a primitive type variable and ‘a’ is also a primitive type. Therefore, JVM just copies the data from a to b.

3. In the third line, we just modified the value of variable b equal to 100. In this case, JVM will store the entry 100 on the stack in the place of.

4. Let’s take some reference type variable i.e. Non-primitive type. In line 4, ‘c’ is a reference type variable that is declared as an integer array.

An 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 above figure. The index starts from 0, 1, 2, and 3. Only data stored in c on the stack is a pointer.

5. 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 address of c’s data to d in the stack memory. So, c and d both will point to the same object on the heap.

6. In the six-line of code, an integer array e is declared which is pointing a new object on the heap.

7. Again, an integer array f is created and pointing to the new object on the heap. In the above figure, you will notice that the values are the same but both e and f are pointing to the different objects on the heap.

In this case, the address of e’s data will not be copied into f in stack memory.

8. In the last line of code, a variable g is declared as a string data type 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 case of reference type, stack holds a pointer to the object on the heap. Here, we have described only basic ideas related to the concept of memory allocation of data types.


Thanks for reading!

If you have any further questions or need more insights, feel free to reach out. Happy coding!


#javadeveloper hashtag#java hashtag#programming hashtag#developer hashtag#javaprogramming hashtag#javascript hashtag#coding hashtag#javascriptdeveloper hashtag#programmer hashtag#softwaredeveloper hashtag#webdeveloper hashtag#python hashtag#coder hashtag#programmingmemes hashtag#computerscience hashtag#fullstackdeveloper hashtag#androiddeveloper hashtag#pythonprogramming hashtag#developers hashtag#backenddeveloper hashtag#developerlife hashtag#frontenddeveloper hashtag#html hashtag#codinglife hashtag#phpdeveloper hashtag#code hashtag#softwareengineer hashtag#programmerlife hashtag#softwaredevelopers hashtag#programmerhumor


要查看或添加评论,请登录

Ankit Dubey的更多文章

  • Life Cycle of Object in Java

    Life Cycle of Object in Java

    Step 1: Creation of .class file on disk As you know that Java programs run on the Java virtual machine (JVM).

  • ?? C++ vs Java: Key Differences You Should Know

    ?? C++ vs Java: Key Differences You Should Know

    Introduction to C++ language C is a general-purpose programming language developed by Bjarne Stroustrup at AT & T Bell…

  • C vs Java | Vital Difference between C and Java

    C vs Java | Vital Difference between C and Java

    Introduction to C language C is a procedural, middle-level, compiled, and general-purpose programming language…

社区洞察

其他会员也浏览了