*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collect from Sarga.com Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collect from Sarga.com Exam" Watch "Garbage collect from Sarga.com Exam" New topic
Author

Garbage collect from Sarga.com Exam

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
At what point is the object anObj available for garbage collection.
01: public class Base{
02:
03: private void test() {
04:
05: String anObj = "sample";
06:
07: anObj.trim();
08:
09: anObj = anObj.toUpperCase();
10:
11: anObj = null;
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
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.
hemanshow
Greenhorn

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.
2.do charAt() as convert each char to upper case.
3.Now u will be modifying teh same object.
right?
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.
vivek
[This message has been edited by Vivek Shrivastava (edited July 20, 2000).]
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi,
Looking forward to hear from our java expert.
regards
vivek
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.....
Regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collect from Sarga.com Exam