• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Garbage Collection

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Given the following code, how many objects will be eligible for garbage collection on the line with the comment //here
( This question is from Marcus green)

public class BettyAck {
public static void main(String argv[]){
BettyAck b =new BettyAck();
}
public BettyAck() {
Integer x = new Integer(10);
findOut(x);
Integer y = new Integer(99);
Integer z = y;
z = null;
findOut(y);
//here
}
public void findOut(Integer y){
y = null;

}
}


Choice :
0
1
2
3
4

Let me know the ans and also the explanation for that
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SCJP questions belong, not surprisingly, in the SCJP forum. Moving...
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is 0 , because when u reach "here"
both the Integer objects are still reachable
You can access them using the references x and y respectively

The method findOut is just assigning the local variable y in that method to
null , but the variables x and y in the constructor will still be
referring to the objects

Hence no objects are eligible

Hope that clears ur doubt

santhosh sharma (scjp 1.4 100%)
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ya, got a clear ans...
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was just wondering if it will be a same case if Integer class is replaced with String

public class BettyAck {
public static void main(String argv[]){
BettyAck b =new BettyAck();
}
public BettyAck() {
String x = new String("10");
findOut(x);
String y = new String("99");
String z = y;
z = null; //~ is z eligible for garbage collection here ??
findOut(y);
//here
}
public void findOut(String y){
y = null;

}
}

"//~ is z eligible for garbage collection here ??"
Please let me know
Regards,
Shakti
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still the answer is 0 ,
both the String objects are still reachable
You can access them using the references x and y respectively. This is true for any type of Object. As the Object is eligible for garbage collection only when it has no reference pointing to it. But it is not true in given code as both the object created have the reference variable to access them.
[ February 20, 2006: Message edited by: gaurav singhal ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need some additional clarification...


public void findOut(Integer y){
y = null;
}



My understanding....
In the above method the argument passed to the method "findOut" is an Object Reference. Meaning its just a some combination of bit-pattern that takes us back to the same object from the calling method.

So Integer y in the method "findOut" and the Integer x in the calling method should be pointing to the same object on the heap as "y" inside "findOut" is only a reference...am I right??

If right then I guess when Integer y in the method "findOut" is set to null then the corresponding Integer x in calling method will also be assigned to null and hence becomes eligible for GC...

Let me know if I am missing something here??

- Mel
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What the function is getting is a copy of the reference, that is, passing the reference by value. So a new copy of the reference is created local to the function. That local copy of the reference is being set to null.

Its same as



Here, s2 is null but s1 still is pointing to the string "Hello". Assume s2 as the local copy in the fucntion and s1 as the reference which is being passsed from the main function.

This is what I think and I may be totally wrong.

regards,
vijay.
 
Melanie Jones
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah I understood how Java works in this scenario....


public void findOut(Integer y)
{
// Line 1
y = null;
}



At Line1 inside the method "findOut" both Integer y and the calling method's Integer x keep pointing to the same location (the same object on the heap)...

Execution of "y = null;" is just way of telling (in K&B words ) OK!! enough of you pointing to same location as x...now point to nothing!! So y is lost to the world but the Integer x still holds the reference!!

Im Clear!!

- Mel
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic