wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage" Watch "Garbage" New topic
Author

Garbage

Sanjeev Gupta
Greenhorn

Joined: Mar 08, 2001
Posts: 15
Which is the earliest line in the following code after which the object created on the line marked (0) will be a candidate for being garbage collected, assuming no compiler optimizations are done?
public class Q76a9 {
static String f() {
String a = "hello";
String b = "bye"; // (0)
String c = b + "!"; // (1)
String d = b;
b = a; // (2)
d = a; // (3)
return c; // (4)
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); // (5)
}
}
a) The line marked (1)
b) The line marked (2)
c) The line marked (3)
d) The line marked (4)
e) The line marked (5)
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
STRING LITERALS ARE NOT GARBAGE COLLECTED.
Rahul Ramachandran
Ranch Hand

Joined: Feb 27, 2001
Posts: 48
Good one!
I do not know.
However as bye is still being pointed to by c, none of the options seem to be right!
Anyone?
Thanks
Rahul
Sanjeev Gupta
Greenhorn

Joined: Mar 08, 2001
Posts: 15
Thanks tvs, that was helpful.
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
so what is the answer to the question
what if a string object is not referenced anymore
like here the object b is not being referenced anymore
i think the answer should be c.
what does the author of the exam say about the answer sanjeev
[This message has been edited by sona nagee (edited May 05, 2001).]


sona<br />SCJP
rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
hi all,
according to me, the answer is d.
B'coz the string objects created are all within the scope of the method.Therefore as soon as the method f() returns to the main() method ie on line 4 of the code, "bye" will be Garbage Collected.Moreover variable c which is returned to main() doesnot
refer to "bye" but to a new object "bye!".
Other than the object "bye!" none of the objects created in method f() are available outside f().hence d) should be the coreect answer.
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
hi rajshree
the question here is asking after which line will the object be eligible for garbage collection.
therefore the answer should be c
if they would have asked when will the object become garbage then d would be correct
here the question is worded differently
Sanjeev Gupta
Greenhorn

Joined: Mar 08, 2001
Posts: 15
Hi Sona,
All variables are referring to string literals. As string literals are not garbage collected, all options are incorrect.
If there were an object created with the 'new' operator, that would have been garbage collected.
The question is from Khalid's free mock exam . It does not explain any answers.
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
sanjeev is 100% right.
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Folks,
Let me have the pleasure of the LONG and MISLEADING discussion in this thread.


All variables are referring to string literals. As string literals are not garbage collected,
all options are incorrect.
If there were an object created with the 'new' operator, that would have been garbage
collected.
The question is from Khalid's free mock exam . It does not explain any answers.



Sanjeev the answer is indeed from Khalids Mock and it is Question # 18 and the answer is (c) why ?? Please refer to
pp 723 of the book.
TVS Sundaram dont have that misconception. If string literal will not be garbage collected then it would have a serious DESIGN ISSUE and LOT of RESOURCES WILL NOT BE USED BY THE JVM!
Let me explain, what happens at each line of execution!

Hope my explanation suffices.

Ravindra Mohan.
Sanjeev Gupta
Greenhorn

Joined: Mar 08, 2001
Posts: 15
Well, chapter 4 of 'Thinking in Java' clearly states that the garbage collector works only for objects created with 'new'. But Ravindra seems to be confident about his explanation. I do not have Khalid's book. I would request one of the moderators to step in and clear the things (or should I say, the 'garbage')!
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
Sanjeev gupta,
U are right that string created with new are eleigible for garbage collection not the string literal's.I had a long discussion on this with one of our moderator named "JANE GRISCTI".SHE said string's created with new keyword are eligible for garbage collection not the string literal's.I had discussed the same thing with ROESANE ZHANG, a moderator OF jchq site,she also told the same thing.
I think RAVINDRA MOHAN u need to consult other's for this.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi guys,
There are a few points here that need clearing up

  1. The JVM specifications do not say how the gc is to be implemented; it is only required to work on objects created in heap memory. String literals are stored in a constant_pool, not in the heap. See JVM §3.5.3, §2.17.7 and §5.1.
  2. Most of the mock exams ignore this and use String literal objects in the gc examples.

  3. In view of the above,

    1. It is unlikely you will see real exam questions on gc that utilize string literals. Sun does not test on features that are system dependent.
    2. When answering a mock exam question involving String literals, assume they are gc'd

    3. In the example question given, assuming string literals are gc'd; the correct answer is 'c' as at that point no reference variables contain the memory address for the string literal 'bye' created in line 0.
      Hope that helps.

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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Sanjeev Gupta
Greenhorn

Joined: Mar 08, 2001
Posts: 15
Thanks Nitin and Jane, that clarifies!
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
That was good clarification by Jane.
SO AS FAR AS THE QUESTION GOES "MY ANSWER" IS "CORRECT".
BUT FROM THE SCJP POINT OF VIEW WE WON'T BE GETTING
QUESTIONS THAT BRINGS AMBIGUITY. QUESTIONS LIKE THESE
IN AN ACTUAL EXAM WILL CONSIST ONLY OF OBJECTS.
IN "MOCK EXAMS" THE ANSWER SHOULD BE BASED ON THE "ASSUMPTION" THAT STRING LITERALS ALSO GET GARBAGE COLLECTED.
HOPE THIS ENDS THIS LONG DISCUSSION HAPPENING IN THIS THREAD.

RGDS,
RAVINDRA MOHAN
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216

[This message has been edited by Ravindra Mohan (edited May 06, 2001).]
James Roddy
Greenhorn

Joined: Jan 16, 2001
Posts: 25
Hi Ravindra,
Pls don't use caps . it is loud & offensive . Remember u'r here not 2 win an argument but 2 help u'rself & help others . try 2 tone down u'r language . it would sure do u a lot of good.
PS:- i don't see how after what Jane has said u win the so called "Argument".
Best of luck,
james
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage
 
Similar Threads
Garbage Collection
Mughal rasmussen
Strings and GC
Garbage collector
Garbage collection