Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Garbage collection...

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I got it from http://www.danchisholm.net/july21/topic/section3/gc1.html

class A {
private String name;
private A otherA;
public A(String name) {this.name = name;}
public void other(A otherA) {this.otherA = otherA;}
public A other() {return otherA;}
public String toString() {return name;}
protected void finalize() {System.out.print(name);}
}
class B {
public static void m1() {
A a1 = new A("A1"), a2 = new A("A2"), a3 = new A("A3"), a0 = a3;
a1.other(a2); a2.other(a3); a3.other(a1); //Is anything prints?
for(int i = 0; i<4; i++){System.out.print(a0 = a0.other());} //What would be the reference returned by this other() in case of each iteration.
}
public static void main(String[] args) {m1(); System.gc();}
}


Which of the following could be a result of attempting to compile and run the program?

a. A1A2A3A1
b. A0A0A0A0A1A2A3
c. A1A2A3A1A2A3
d. A1A2A3A1A1A2A3
e. A1A2A3A1A3A2A1
f. A0A1A2A3A1A2A3

Answer:a,c,d,e.

really a hard job to find the logic going here. it would be a great help if anyone explain me this clearly.

Thanks a lot in advance

Preparing Scjp 1.5
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags otherwise it is very difficult to read a code...
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
private String name;
private A otherA;
public A(String name)
{
this.name = name;
}
public void other(A otherA)
{
this.otherA = otherA;
}
public A other()
{
return otherA;
}
public String toString()
{
return name;
}
protected void finalize()
{
System.out.print(name);}
}
class B {
public static void m1()
{
A a1 = new A("A1"), a2 = new A("A2"), a3 = new A("A3"), a0 = a3;
a1.other(a2);
a2.other(a3);
a3.other(a1);
for(int i = 0; i<4; i++)
{
System.out.print(a0 = a0.other());
}
}
public static void main(String[] args)
{
m1();
System.gc();
}
}

could you read it now?
 
Ankit Garg
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Preetha this is a tricky question...

when this code is executed



the output will be A1A2A3A1.

This is because when the loop iterates for the first time, a0 is a3. So a0 = a0.other() will make a0 as a1 (as a3.other() is a1). So the toString method will be called on a1 and A1. Then again a0 will be assigned a1.other() i.e. a2. So toString will be called on it and A2 will be displayed. Similarly A3 and A1 will be displayed.

After this the method call will return and all the objects will be eligible for garbage collection. calling System.gc() will ask for garbage collector to run(which is not guaranteed). So after A1A2A3A1 A1, A2 and A3 can be displayed 0 or 1 times. This is the reason the given options are possible...
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting ,but stuck on how a3.other() returns the reference and on what basis?. i am confused with the methods void other(A otherA) and A other().
Actually A other() returns the reference which void other(A otherA)has assigned . am I right here?
In the for loop :
a3.other()-->returns A1(as you said) and similarly for a1.other(),a2.other(),a3.other().
please clear me how the reference is coming from this method.

Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic