File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC doubt 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 "GC doubt" Watch "GC doubt" New topic
Author

GC doubt

sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257

The answer is D. Can someone explain how? What happens when r=r+1 is done?
Thanks


Sunita<br />SCJP 1.4
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Originally posted by sun par:

I think it is obviously D.
A,B,C are wrong. S is local variable, so it will be collected when the method returns. So E is wrong as well.
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Thanks Don. I somehow read the question as when r will be garbage collected. When will r be garbage collected?
Thanks
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I think D too.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
In response to the second question, the object referred to by 'r' will be eligible for collection after the statement r = null;
Not to be picky but the language is important - you can't ever say that an object will be garbage collected, only that it is eligible for garbage collection. The exam will try to trick you with phrases like 'when will the object be collected' and the answer is - you can never know.
But you can know when an object is eligible!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I think the definition of 'eligible' is not just being null, it also including 'not refered again'. So I think here D is correct.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
r and s are referring to two different String objects created from the String literal "abc".
At s = s + r; the original String object referred to by s is replaced by a new String object formed by concatenating the String objects referred to by s and r. The original String object referred to by s is then available for GC. So it is before statement labeled 4.
As for r, r = r + 1; The String object originally referred to by r is replaced by a new String object formed by concatentating String.valueOf(1) to the String("abc") object. So the original String object referenced by r becomes eligible before statement labeled 2.
Just my addition to the confusion
-Barry
Sorry for all the edits, the brain is acting up again. I do not always type what I mean anymore.
[ December 26, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I think GC applys to variable, not to string, int, float....
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Garbage collection refers to instances of classes, that is objects. We are talking about String objects referred to by variables.
-Barry
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
If I remember correctly, one of the conditions for GC is 'won't be refered again'. This makes sense to me.
In the example above, if r is GC'ed after statement 2: r=null, then what will happen when s=s+r come along.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
r is NOT garbage collected, the String object that r originally referred to will be eligible for garbage collection; that is, an object String("abc"). r refers to a new object a String("abc1"), before it set to null.
Setting r to null then makes the String("abc1") object become eligible for garbage collection.
s = s + r, causes s to refer to a String("abcnull") and releases the String("abc") originally referred to by s to become eligible for garbage collection.
-Barry
Once again apologies for edits.
[ December 26, 2002: Message edited by: Barry Gaunt ]
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I see your point. But I just wondering what will happen if after "abc" originally refered by r was garbage collected, we declare another variable:
f = new string("abc") ;
Wouldn't it be nice if we can reuse 'abc'?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It does, sort of. The String literal "abc" is held in a JVM pool. The f = new String("abc") object is created from that pooled string literal. But it is a totally different object to other String("abc") objects as a test with == will show.
-Barry
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I am tempted to ask another question:
What is exactly GC?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Memory to hold objects is stored inside the JVM in a data-structure called a Heap . If a situation arises where there is no longer a reference to an object's memory, then the memory can be reclaimed so that the memory can be utilized for storing other objects. This reclamation process is performed by a Garbage Collection algorithm running as a low priority thread inside the JVM. When the garbage collector thread runs you cannot, as a programmer, predict. What algorithm it uses you cannot, as a programmer, predict; it is implementation dependent. A programmer can only say that an object, after some point in the code, has no references and, in that case, is eligible for garbage collection when and if the garbage collection algoritm wants to do it.
For more I refer you to the literature. I retire to bed.
-Barry
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Thanks Barry, Don, Bert
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC doubt