This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about Garbage Collection" Watch "Question about Garbage Collection" New topic
Author

Question about Garbage Collection

Jo Liang
Greenhorn

Joined: Dec 10, 2001
Posts: 11
How many objects are candidates for garbage collection by the end of the following code sinppet:
1.String s = "Hello";
2.s = "Hello" + " World";
3.System.out.println(s.toUpperCase());
A) 1
B) 2
C) 3
D) 4
E) none

This answer is B.
Can somebody explain why? Thanks
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
Originally posted by Jo Liang:
How many objects are candidates for garbage collection by the end of the following code sinppet:
1.String s = "Hello"; //1
2.s = "Hello" + " World";//2
3.System.out.println(s.toUpperCase());//3
A) 1
B) 2
C) 3
D) 4
E) none

This answer is B.
Can somebody explain why? Thanks

Hello Jo,
I think the answer shd be 'a' ie. only 1 object...
Have a look at code sequence...(to which i have given number)
1. at line 1, String literal "Hello" is created in literal pool
2. at line 2 Compiler sees that "Hello" is already present in literal pool. so it doesn't create it. Only String literal"World" is created
in pool
Now at runtime, because + operator, a string object is created on heap. ie. String object containing "HelloWorld"
3. Now at line 3, because of function toUpperCase() new string object containing "HELLOWORL" is created.
Now s is refering to the above object . so the previous object (containing "Hello World") is unreferenced now. so it can be garbage
collected.
Here 2 obejct in string pool and 2 in heap r created. and s refers to one of the heap object
so only one remains for garbage collection. String literals r never garbage collected.
Correct me if i am wrong
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
Well, given the information in the assignment, this is just a snippet, so the code doesn't end there. That means that the text "HelloWorld" still has a reference to it. However, the "Hello", and the uppercase of "HelloWorld" does not (we're only printing the uppercase version, not changing any references. Remember: String is imutable).
/Mike
Simeon Shi
Ranch Hand

Joined: Dec 09, 2001
Posts: 35
hi,Mike...
i agree with u but one more question:
what about the "world" obj?
i think it also should be gc.
am i right?
Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
Hi all,,,
simoen u r perfevtly all right.....
------------------
Muhammad Hussain

Muhammad Hussain<br />Sun Certified Java Programer (SCJP2)<br />Sun Certified Web Component Devloper SCWCD
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Are they really garbage collected.
They are String literals and according to our discussion String literals are not garbage collected.
So my ans would be none.
Please correct me
-Neha


nss
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
2 is the correct answer because:
s = "Hello" + " World"; is going to create a new String dynamically and s.toUpperCase as well. So we end up with to dynamically created Strings which are to be gced !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
String literals are not garbage collected.
"hello" + "world" is computed at compile time because is a constant as defined by JLS 15.28. So is not garbage collected either. Yes the string retuned by to UpperCase is ready for collection.
Many mocks doesn't actually take into account the fact that String literals are not g.c.ed . Just try answering as if they were normal String. In the exam I don't think we will be asked for the collection of String literals.
Use a WeakReference object in case you are interested in checking that String literals are not g.c.ed . Search the Ranch for my name and you will see previous examples.


SCJP2. Please Indent your code using UBB Code
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
then the answer will be A.
Since s.toUppercase() has no reference it will be gced.
Am i right
-neha
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Sorry! Jose is right
s = "Hello" + " World"; is a compile-time constant and thus is not gced...
my mistake!
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Tony Sam
Ranch Hand

Joined: Nov 19, 2001
Posts: 32
Originally posted by Jose Botella:
String literals are not garbage collected.
"hello" + "world" is computed at compile time because is a constant as defined by JLS 15.28. So is not garbage collected either. Yes the string retuned by to UpperCase is ready for collection.
Many mocks doesn't actually take into account the fact that String literals are not g.c.ed . Just try answering as if they were normal String. In the exam I don't think we will be asked for the collection of String literals.
Use a WeakReference object in case you are interested in checking that String literals are not g.c.ed . Search the Ranch for my name and you will see previous examples.

Perfect , i agree with Jose

Tony Sam<BR>One want to be a SCJP...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Garbage Collection
 
Similar Threads
Grabage Collection
GC
Question regarding Garbage Collection
Garbage Collection
GC questions