Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


himanshu bhardwaj
java
3 Answer(s)      2 years and 2 months ago
Posted in : Java Beginners


class CardBoard { Short story = 200; CardBoard go(CardBoard cb) { cb = null; return cb; } public static void main(String[] args) { CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = c1.go(c2); c1 = null; what is the memory allocation of the program ?


Advertisement
View Answers

July 5, 2012 at 4:45 PM


We are creating two objects and they are referred by reference variables c1 and c2.

During the execution of CardBoard c3 = c1.go(c2);, no new objects are created in the heap. The object c3 is originally null, so there is no question of recollecting it, for it never existed in the first place. The garbage collector is meant to scavenge objects that actually exist on the heap.

The memory allocated for c1 can be garbage collected after line c1 = null; But the total number of objects garbage collected is 2 since the CardBoard class contains one wrapper object Short.



July 5, 2012 at 7:31 PM


1.yes object c3 is null as it will not get memory in heap. C1 & C2 are themselves objects what does this means that they are reference variable such that they are referring the new memory allocation in heap.

  1. CardBoard go(CardBoard cb).? what does this statement means ..? according to me may be it is saying that go is a function and cardboard is a return type. and in this ()cardboard cb is an parameter of type cb and named cardboard.

3.c1.go(c2)..what is c2 in this line.?

4.c1 is null as it is equals to null.but how c2 is null and collected by garbage.does not it goes to heap.or how this is equal to null.

anyways thanks for the replies to my queries.



July 5, 2012 at 10:20 PM


CardBoard class contains one wrapper object Short. what is the meaning of the above line please explain ?

Do reply..



Related Tutorials/Questions & Answers:
 

 

 

DMCA.com