aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objects eligible for garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objects eligible for garbage collection" Watch "Objects eligible for garbage collection" New topic
Author

Objects eligible for garbage collection

sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
Hi,



Objects eligible for garbage collection after Line 1: 3(ans given)

But according to me,it should be 1.
As a is assigning value to b then c<=b then d<=c.
I know that my logic might not be right.

Please correct me.

Thanks
Gilles Marceau
Ranch Hand

Joined: Feb 17, 2007
Posts: 78
Hi,

the assignments are made from right to left, it means after :
d=c=b=a;
all the object references are now refering to the first created object.
It means the three others created objects are no longer reacheable by
a runnable thread (this is the condition to be eligible for garbage
collection).
Setting the d reference to null doesn't change this fact : at line 1,
the three other objects instance are eligible for garbage collection.

Hope this helps

Gilles


SCJP 1.5<br />SCJD 1.6<br />SCBCD in progress...
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
I didn't get you.
Can you please elaborate for me please.


Thanks<br />Dinesh
Gilles Marceau
Ranch Hand

Joined: Feb 17, 2007
Posts: 78
Hi,

what is the point you don't understand ? (be more accurate, because
"i don't understand" is not a good starting point for a more detailed
answer).

Gilles
Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
code:
-------------------------------------------------------------------

public class Test{
public static void main(String[] args) {
Object a = new Object(); // the object original referenced by object reference a
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c=b=a;
d=null; } } //Line 1
-------------------------------------------------------------------------

Ok let me explain it..for understanding take
Object a =new Object(); as a new Object is created Of type Object and a Object reference variable a is assigned to it.(say this object as ObjectA).Similary for
Object b = new Object();
Object c = new Object();
Object d = new Object();
three Object will be created(say ObjectB,ObjectC and ObjectD ) and will be assigned some Object references.

d=b=c=a means all four references will now point to ObjectA(initially referenced by reference a ). It means we dont have any reference to access ObjectB,ObjectC and ObjectD. So they are eligible for garbage collection.

Finally
d=null makes variable d to point nothing. So total three Object are eligible for garbage collections.

If i m wrong,please experts correct me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects eligible for garbage collection