File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collect from Exam 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 "Garbage collect from Exam" Watch "Garbage collect from Exam" New topic

Garbage collect from Exam

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
At what point is the object anObj available for garbage collection.
01: public class Base{
03: private void test() {
05: String anObj = "sample";
07: anObj.trim();
09: anObj = anObj.toUpperCase();
11: anObj = null;
12: }
14: static public void main(String[] a) {
15: new Base().test();
16: }
18: }
Select most appropriate answer
a) At line 5
b) At line 7
c) At line 8
d) At line 10
e) At line 12
The answer given is d. But I marked 'e'. Could anyone pls. explain me which is correct.

Joined: Jun 26, 2000
Posts: 25
Ofcourse answer is d. Coz after line 9, anObj refeers to a new object which is anObj.toUpperCase(). Remember string objects are immutable, u can't change anything in object. It seems u r changing the same object to uppercase by in reality new string object is returned by method toUpperCase().
Now think the scinerio:
1.Create anObj as StringBuffer. charAt() as convert each char to upper case.
3.Now u will be modifying teh same object.
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi hemanshow,
I do not agree with your answer because before line#9 anObj refer to a String object created on 'String Pool'. we all know that string object cretaed on 'String pool' are not available for GC.
so when we create a new object on line#9 it is being created on heap. so it is available for GC after line#11.
java expert please do verify it. please correct me if am wrong.
waiting for reply.
[This message has been edited by Vivek Shrivastava (edited July 20, 2000).]
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Looking forward to hear from our java expert.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
While I wouldn't really qualify myself as a java expert,
anyways coming to the qstn here I go...
I have seen in the previous post (abt 4 mths back) it was
advised on the ranch that for the purpose of SCJP exam,
we should forget abt the concept of String pools.
Once we forget that, the other concept is (my first lesson
from Maha) "anObj" is a reference to an object and the
object is what gets GC'd.
Thought the qstn is ambiguous (which obj is to be GC'd?
the obj sample or the uppercase obj SAMPLE)
in such cases, assuming the author refers to the obj
sample the answer would be d.
Why the assumption, i have no idea...I told you my ambiguity
abt the qstn before....
Hopefully we agree.....
- satya

Take a Minute, Donate an Hour, Change a Life
I agree. Here's the link:
subject: Garbage collect from Exam
It's not a secret anymore!