This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Garbage Collection

 
Jo Liang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,,,
simoen u r perfevtly all right.....
------------------
Muhammad Hussain
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic