Zero is correct. Line 1 creates a new integer object. In line a copy of refernce x is passed to method. In the method findOut, the copy is reassigned , not the original object. The original object pointed by x stil exists. Line 4 causes one more reference to point to object created at line 3. z =null makes only y point to the object. Apply similar logic as applied previously for findout(y). When the commented line is reached all objects are refernced and are not eligible for GC. Hope this helps
Hi, According to me also it should be 2 only. Lets see if any one can explain !!
Manish Nijhawan
Greenhorn
Joined: Apr 04, 2005
Posts: 10
posted
0
Hi Pardeep, As you have explained that at the commented line since all the objects have the references so they are not eligible for the GC. But since that line is the last line of the method , so after that line all the references to all the objects will vanish , so all the Live objects should be eligible for GC.
Please explain me this as i am very much confused about GC.
Manish Nijhawan<br />B-Tech
Srinivasa Raghavan
Ranch Hand
Joined: Sep 28, 2004
Posts: 1228
posted
0
I agree none are eligible for GC.
But if the findOut() is modified like the one below then 2 objects are eligible for garbage collection , am i right ?
[ April 21, 2005: Message edited by: Srinivasa Raghavan ]
Originally posted by Manish Nijhawan: Hi Pardeep, As you have explained that at the commented line since all the objects have the references so they are not eligible for the GC. But since that line is the last line of the method , so after that line all the references to all the objects will vanish , so all the Live objects should be eligible for GC.
Please explain me this as i am very much confused about GC.
The objects will be eligible for GC after the method completes.
soumya ravindranath
Ranch Hand
Joined: Jan 26, 2001
Posts: 300
posted
0
Hi,
I understand that the object holding 111 in findOut() becomes eligible for GC, but which is the other object that's eligible after the modified findOut() ?
According to me only one object will be elgible for garbage collection..i.e new Integer(10); this object has refernce count =0;as u can check .Intially this object was refrenced by x but when x=y is executed .This object is no longer refrenced. new Integer(99) is being still refernced by y.so only one object is elgible for garbage collection
Originally posted by nish vats: According to me only one object will be elgible for garbage collection..i.e new Integer(10); this object has refernce count =0;as u can check .Intially this object was refrenced by x but when x=y is executed .This object is no longer refrenced. new Integer(99) is being still refernced by y.so only one object is elgible for garbage collection
Where is x=y I dont see it. What about others?
soumya ravindranath
Ranch Hand
Joined: Jan 26, 2001
Posts: 300
posted
0
As I see it, there is no x =y in the code given.
When z = y and then z = null, as Pradeep said, it means nothing to z or y. x continues to point to 10.
Paulo Aquino
Ranch Hand
Joined: Apr 29, 2002
Posts: 200
posted
0
Now I understand why "0" was the answer. The copy of the reference object was reassigned in findOut method and not the original reference.