This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes gc ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "gc ??" Watch "gc ??" New topic
Author

gc ??

Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
I found this question at MindQ
36. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
any comments/answers???.
--Farooq


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
My comment is that questions about GC should NEVER use String as an example due to the complication introduced by the String constant pool. In this code no new String objects have been created so there are none that are eligible. "Nick" etc are all constants created when the class loaded.
Bill
------------------
author of:
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Muhammad,
Bill is correct; string literals are not gc'd however many of the mocks use them in gc questions.
Here's an earlier thread on the same question that explains the question.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Thanks Bill and Jane,
I appreciate your help, I found this question on MindQ and the answer there was one object.
--Farooq
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
How many objects are eligible for gc after the last line??
String a = new String ("Hello");
String b = new String ("World");
String c = a + b;
a = null;
b = c;
c = null;
b = c;

AAA
--Farooq
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

I think it should be 2 Farooq!
------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
I also think two objects are eligible for GC.
Pls. correct if wrong ..

How many objects are eligible for gc after the last line??
String a = new String ("Hello");
String b = new String ("World");
String c = a + b; // c contains "HelloWorld"
a = null; // as a has no more reference, eligible for GC
b = c; // b contains "HelloWorld"
c = null; // c has no reference, but b still contains "HelloWorld"
b = c; // b now contains null reference , so it is eligible
for GC

swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
This is what MindQ TBTs say about String literals

String literals, which are enclosed in double quotation marks, create an instance of a String class object whose contents cannot be changed.

is it correct? I know, there had been lot of dicussion on this topic..but still i seems to bring confusion with every new post..
Thanx

Swati Kale
SCJP
SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: gc ??