The memory location of Primitive and Non-primitive data types


In this blog, I intend to give a brief about memory locations of both primitive and non-primitive data types.

Let's just dive right in, and figure out how this works.

Your program has a certain amount of memory allocated to it. This is divided into the "heap" and "stack."

Primitive data types

Primitive values have to be stored somewhere in our memory, and it is Stack. It is the type of Memory, which can be accessed real quick and which is also very limited in space too, it doesn't hold much information and but due to the nature it was built or due to how it works it is really fast. so it's perfect for primitive values like strings, numbers, etc, that aren't too complex, which don't take up much space.

Let's say we already have a random text and number in there now we create a new variable firstName "Max", therefore we store this string "Max" somewhere in the memory or on top of the stack and we eventually know that this variable is accessing this part of the stack/entry.

Screenshot 2020-09-20 at 1.09.17 PM.png

Now if we assign the firstName variable as a value to a new variable, let' name it fullName then a new entry is pushed on top of the stack and we that this variable is referring to the topmost entry in the stack.

So this is how we manage the stack or how data gets managed on the stack with different data pieces and our variables know which position in stack they are referring to you could say.

Non-primitive data types:

Non-primitive values are stored in a Heap.

And let's take a look at how the heap works, the random data in the yellow blocks in the given illustration are just the heap representation, and that's a place in memory where elements are not stored on top of each other it's not managed like a Stack, it's managed randomly you could say and therefore each element has it's own address and we still have a stack of course.

Screenshot 2020-09-18 at 11.15.45 PM.png

As you will see now, we create a new object the player1 and the following happens, the element is created in the heap which stores the actual object but we do have a Pointer on the stack which stores the reference to the address to this object in the heap and the variable simply stores the Pointer and that's the interesting thing, the variable doesn't know the address of the place in memory where our object is stored, that's the difference to the previous approach, but the variable knows where the pointer lies on the stack, it knows the position of the pointer, on the second when the Pointer in turn simply stores the address of the object in the heap.

Screenshot 2020-09-20 at 12.44.53 PM.png

Now if we create a new variable player2 and we store player1 as a value this creates a new pointer on the stack, but and that is not a key thing, this pointer points to the same place in the memory as the first pointer did, so therefore unlike the previous example where we managed primitive types on the stack.

If we assign the value to a new variable the actual value, the object here is not copied as it did in the stack, it's still the same. But we do get a new pointer pointing to the same place, and it's two different pointers that are stored in two different variables but pointing to the same value.

Screenshot 2020-09-20 at 12.41.38 PM.png

Hope this was useful! Let me know in the comments.