Variable and Memory Address

Ting
4 min readNov 22, 2022

--

Basic

Before we start the main course,it’s necessary to understand that the memory area can be divided into three parts at the execution time.

1.Global

Used to put global variables and static variables. Global variables is one declared at the start of the code and is accessible to all parts of program. Static variables is variable which is belongs to the class and initialized only once at the start of the execution.

2. Stack

The operating system will automatically manage this area, and if it allow the system to manage the information in Stack area clearly, it means that the life cycle of the information stored in it must be calculated in advance.

Stack area is mainly used to store local variables,method parameters and method return addresses.

The above-mentioned things can determine the life cycle at compile time, so the system will manage when to recycle the resources.If the stack area is not enough or the recursive function is not written well, a StackOverflow Error will be generated.

3. Heap

Something are predictable we put in stack area , so what about something are unpredictable where we put in ? It’s Heap area.

Why is there unpredictable data? Actually in many cases,the program doesn’t know how much memories to use until it executed, furthermore it even doesn’t know when it won’t be used .

In Java or C++: All of objects we use “new” to create will be stored at Heap .

In C : malloc() or calloc()

The data system here won’t be automatically reclaimed, so as the program executes ,the memory will become less and less. Therefore, we must manage the memory in this area by ourselves ! Here we could find out that Java is very considerate and has a mechanism to help us clean up this heap area without ourselves. The mechanism is called “Garbage Collection ” which will help us check out which part of memory is no longer in use , and it’ll help us release that part of memory space. So if you are a Java programmer, you can just focus on your program, on writing your code. If there’s not enough space in heap area, an OutOfMemoryError will be generated.

Ok ! After understanding the above knowledge, let’s focus on the memory allocation of stack and heap.

Declaration and Value assignment of Basic data types (int for example)

int value;
value = 20;

Memory allocation diagram:

The program has just started to execute, and there are no any variables in the memory. When executing “int value;” this code, an int size space will be cut in the stack area. Here we need to know that Java will initialize it to 0, but the local variable won’t initialize automatically, so programmers must specified before using the local variable. And then execute “value = 20” to assign the integer 20 to the variable value.

Declaration and assignment of reference data types

Here we postulated on having a class “Animal”

Animal fox;
fox = new Animal();

Memory allocation diagram:

First,the left one stack memory is expressed as initial status of the program which are no variable in the memory .

“Animal fox” declared a variable fox ,which will cut out a reference size space in the stack area.This reference is expected to point to the Animal object ,and Java will initialize the content to null (pointing to nowhere) .

“fox = new Animal()” This statement is divided into two parts.

  1. First the keyword “new” constructs the object through the constructor, and the object which constructs by constructor will placed in the heap area.
  2. Second is assign the start address of the newly created object in the heap area to the variable fox by specifying the operator “=” . From now on ,the variable fox got the memory address 0100H just like the above diagram shown.

That’s why Java needs “new” this keyword to create an object. If we don’t use this method to create a true object in the heap area , it’ll just a “reference“ in the stack area ,and the default value is null. If a null object variable is accessed, a NullPointerException will appear, which is also a common programming error. All in all, to become a good programmer ,it’s necessary to realize how to manage the memory and all of memory’s allocation!

Hopes that this article could help understand more about memory allocation in Java .Furthermore,this concept is similar to C’s pointer.It’s great to use the idea of pointer to realize today’s concept.

Welcome to comments below to let me know your problems or ideas. Thanks for your reading 🫡

--

--

Ting

AI engineer |Prompt Engineer | Machine learning Engineer|backend developer