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 again ?? 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 again ??" Watch "GC again ??" New topic
Author

GC again ??

geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
From the following code how many objects are eligible for garbage collection?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0

The answer is given as 1.
I thought it would be 0,as strings created in this code are literals.
Am i right?Pl.clarify.
Is there some tips for solving such GC related questions?
Thanks in advance.
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
You are right. It should be 0. Who gave the question saying it is 1 ?
By the way, if a sting is created with new instead of a direct assignment like this :
String s = new String("Hello");
Is it a candidate for GC ?
Thanks.
geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
This question is from one of the mock exams :
http://www.javacaps.com/scjp_mockexams1.html
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
I think that the only kind of object that is liable to be GCed is object. Things like instance of a class or array. So, Integer the wrapper class type could be GCed while int won't be. I think that string literal are kept in string constant pool, which is not subjected to GC therefore the answer should be zero. As for string created using new, that would be creating an object of String type and so it should be eligible for GC.
Correct me if I am wrong. I got these from Passport series on Java 2. It also says that a lot of mock exam asks this types of question and that for the purpose of taking them assume string literal could be GCed.
Hope this helps.


Let us be showered in the light of confusion!
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
The answer is 1 object eligible for GC!
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
after the assignments, then there is no reference pointing to Test. therefore "Test" is eligible for GC. since both string1 and string2 point to the same object i.e. Today!


Amir
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Um...I read somewhere that the only memory space that GC takes care of is the heap which contains only object and array. So, since string literal is contained in string constant pool that is not considered to be inside of the heap, string literal were not suppose to be 'recycled' by GC.
Would some one check this out?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Chung Huang is correct. The mock exam is wrong. However, String pool questions are not covered on the exam.
[ July 01, 2002: Message edited by: Marilyn deQueiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC again ??