Sakib Sami' Blog


Newbie programmer, Java fan, Rosogolla lover, Mom's little kid.


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 about what is inside Java ? How everything happens inside Java ?

If you are familiar with Java then you have already know how Java application starts. From main method,

public static void main(String args[]){  
    // Rest of instructions here
}

And in memory it becomes,
** Red mark space is reserved for main() method.

Application starts running within a thread by default. Java uses stack memory ( green marked ) to handle this.
Lets declare a variable of primitive type,

int x;  
int a;  

It will look like,
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 main method such as,

public static void main(String args[]){  
    check();
}

public static void check(){

}

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, lets have an instance of it in check() method.

public static void main(String args[]){  
    check();
}

public static void check(){  
    Book b = new Book();
}

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 keep 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 c, b value will updated too. Because everything you pass is Java other than method parameters is pass by reference. Method parameters are pass by value.

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

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

public static void check(){  
    Thread t = new Thread(){
        public void run(){
            background();
        }
    };
    t.start();
}

public static void background(){

}

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 stack. As it has been called from second thread.

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

We have a String variable p1 in check() method having value abcd and creates a memory space in heap, then we have declared p2 having 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 equals method 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.

Angular2 async http request

Imagine your are developing a real time data processing app where you have to do chained http reques…

angular2, http_request, angular, rxjs, async

Use Erlang on Embedded System (RaspberryPi)

Assume you have a debian based system (Raspbian, Ubuntu Mate, Ubuntu LXDE etc) ready, Now in /etc/ap…

erlang, raspberrypi, embedded system