Email: info@jugbd.org

Steak Inside || Java Inside

Steak Inside || Java Inside


Steak Inside is a burger item made with a large piece of chicken, cheese, and other things. I am a big fan of it.

Ah, this is not our today’s topic. Today we will discuss what is inside Java? How does everything happen inside Java?

If you are familiar with Java then you have already know how Java application starts – from the main method,

And in memory it becomes,

** Red mark space is reserved for main() method.

An application starts running within a thread by default. Java uses stack memory ( green marked ) to handle this.
Let’s declare a variable of primitive type,

It will look like,

Which means x and a will take place in main() method’s memory. That’s why you can’t access local variables of a method from another method.

Now if we call another method from the main method such as,

Now, what will happen? Any idea? Ok. This is the inside picture.

Different space will be allocated for check() method.

Assume we have a class,Book let’s have an instance of it in check() method.

So it will create an instance in heap memory and keep a reference in check() method’s memory.

Java puts primitive types directly in local memory but objects in heap and keeps a reference to it in method memory.

If we declare Book c and assign b to c c = b; it will create a reference to b‘s object. So if you change anything of cb value will be updated too. Because everything you pass is Java other than method parameters is passed by reference. Method parameters are pass by value.

After completing execution of the method check() automatically it will be removed from the stack as well as all references.

Well, one thread is not enough for us, we need another thread.

And this will happen at memory level.

As we are running a different thread so it will create a different stack to handle everything of it such as background() method in thread 2 stacks. As it has been called from a second thread.

If you are a Java fan then you should know that String is immutable in Java. Immutable means you can’t change it after creation. One of the reasons of it is to save memory space. How ?

We have a String variable p1 in check() method having a value abcd and it creates a memory space in heap, then we have declared p2 havi the g same value abcd. Now it won’t create another abcd in heap rather get a reference to it. When method execution complete only references will be removed not the object.

Do you know how == operator and equals() differs in Java ? == checks for references but equalsmethod checks value.

Now you may want to know how Garbage Collector ( GC ) cleans memory. Ok, it looks for unreferenced objects in heap and if found any GC removes it from memory.

Have any Question or Comment?

One comment on “Steak Inside || Java Inside

Comments are closed.

5 mistakes Java developers make that prevent them from working on awesome projects

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,194 other subscribers

%d bloggers like this: