Showing posts with label Java Basics. Show all posts
Showing posts with label Java Basics. Show all posts

Tuesday, October 28, 2008

Heap n Stack

To explain the answer to my earlier post - "Simple One But...", first the difference between two memory areas in Java - heap and stack has to be understood.

Whenever a program runs in JVM, different ways the memory is managed is depicted in the figure below.

Now coming back to the earlier question, so whenever a methos is executed, it creates a new data record on Stack. So each instance of method call has a exclusive data record associated with it. (Figure 2) . And when a method calls other methods, records of the called methods are stacked up along with the caller. (Figure 3).

This explains or answers for the below, as static methods would be no different from non-static methods, but there is a catch here. This statement is correct only if the arguments are primitives, but when they are objects, the objects would still reside on Heap and are referred from within the stack. (Figure 4)
So finally remembering where a particular element lives, heap or stack:: a local variable (primitive or reference) belongs to a method and lives with it on a stack, while instance variable belongs to an object lives with it on a heap. Also note that a local reference variable on a stack will still be pointing to object in the heap. This object will not die with the local reference variable or post method execution.

Note: figures displayed are extracted from book (in Google Book Search) - SCJP Exam for J2SE 5 by Paul Sanghera and from lecture slides of Gerald Weber.

Tuesday, October 21, 2008

Simple One but ....

In the past 5+ years of my experience as a Jave Panelist for the recuritment drives in company, most regular question by me is this

What is static? What does it mean when a method is declared static - Ofcourse the most obvious answers would be - Static means only one instance per class. And when method is static, only one instance exists for that class. - Immediately my reaction would be a sharp smile as the candidate fell into trap.

My next question is - Then i have a method called add as below.

public static void add (int a, int b) {
int sum = 0;
sum = a + b;
System.out.println("Sum equals -- " + sum);
}

What is the result when two concurrent threads access the same method with inputs (3,4) and (4,5).

Will the execution gives right results or wrong results? if so why?
about 99% of cases have this wrong by first saying right results and when i point back to their definition of having a single instance and stressing on 2 concurrent threads, answer changes to defining the method syncronized. Then to further test his reasoning, i would say, so do you mean every method declared static should have syncronized as mandatory. Bowled!!!!!

Any tries are welcome..will exlain later...

Note: this is just to explain/test a simple java basic, not that i am saddist with the candidates. Having got bored asking typical questions as listed in many websites - java intervew questions etc., try asking un-orthodox questions.