MemoryTest1

package education.jtrainer;

public class MemoryTest1 {

        public static void main(String[] args) { 
            int number=10; // this is created in the Stack
            Integer numberObj=new Integer(10); // this is created in the Heap,  numberObj reference is kept in the stack
            MemoryTest1 memoryTest = new MemoryTest1(); // this is created in the Heap, mem reference is kept in the stack
            memoryTest.print(numberObj); // a block in the top of the stack is created to be used by print(numberObj) method. Since Java is pass by value, a new reference to Object is created in the print(numberObj) stack block 
            // at this point stack memory block allocated for print(numberObj) in stack becomes free.
            // at this point stack memory block created for main() method is destroyed. Also the program ends at this line, hence Java Runtime frees all the memory and end the execution of the program.
        }
         
        private void print(Integer param) { //param is a reference to an object on the heap
            System.out.println(param);
        }
    }