This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Raj Kumar
Ranch Hand

Joined: Sep 19, 2005
Posts: 65
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

SCJP questions belong, not surprisingly, in the SCJP forum. Moving...


[Jess in Action][AskingGoodQuestions]
santhosh sharma
Greenhorn

Joined: Jan 29, 2006
Posts: 20
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

Joined: Sep 19, 2005
Posts: 65
Thanks ya, got a clear ans...
Shakti Dalabehera
Greenhorn

Joined: Sep 17, 2005
Posts: 5
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
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
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 ]
Melanie Jones
Greenhorn

Joined: Dec 16, 2005
Posts: 21
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
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
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

Joined: Dec 16, 2005
Posts: 21
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Garbage Collection
 
Similar Threads
confused............
can anyone explain this?
garbage collection
GC Question from Marcus Green
Garbage Collection