Forums Register Login

Question in Garbage collection

+Pie Number of slices to send: Send
Please explain me the output of the following snippet(if possible with a diagram).

class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
}
public static void main (String[] args) {
new J().m1();
}}

Which object is not eligible for garbage collection after method m1 returns?

a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above

The answer is None of the above.
+Pie Number of slices to send: Send
Since all the objects are created within the heap(in this case , the other happens to be an object of type I, the answer "None of the above is correct".
+Pie Number of slices to send: Send
Shankar,
You can refer to KBs book (pp 435 - Section "Isolating a Reference" ) talks in depth

Thanks
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 553 times.
Similar Threads
GC
Dan's Question on Garbage Collection
garbage collection
Garbage Collection
reg dan's test suite
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:47:23.