Hi All,
PFB the first piece of code.
# 1. public class Garb{
# 2. Garb g;
# 3. public Garb(Object g){
# 4. this.g=(Garb)g;
# 5. }
# 6. public static void main(
String args[]){
# 7. Garb gb1 = new Garb(new Garb(new Garb(null)));//line 1
# 8. gb1.g.g=new Garb(new Garb(null));//line2
# 9. Garb gb2=new Garb(new Garb(new Garb(gb1)));
# 10. gb2.g=gb1.g;
# 11. System.gc();
# 12. }
# 13. }
Here is the second code.
# class A{
#
# A a1;
# A a2;
#
# public void finalize(){
# System.out.println("-");
# }
# public static void main(String args[]){
# A s1=new A();
# s1.a1=new A();
# s1.a2=s1;
# s1.a1.a2=new A();
# s1.a1.a2.a2=new A();
# s1.a1.a2.a2.a1=s1.a1;
# s1.a2.a1.a2=null;
# System.gc();
# }
#
# }
I have picked these codes from this forum only but was unable to get through these questions as how we are deciding which object is going to be collected by Gc.
Can somebody please explain me the fundamental/logic as how to solve these kind off questions ?
Regards,
Arun Yadav