Originally posted by Paul Salerno:
Hi guys, I wanted to further tighten up my notes on the subject of GC and strings in the literal pool. I know Val touched on this earlier, but I need to make sure I'm on the right track
Case 1:
Case 2:
if one of my friends could show me where I'm wrong on this, I'd greatly appreciate it.
Rob
SCJP 1.4
Originally posted by Rob Ross:
S = "Kanga" + "Roo"
Both "Kanga" and "Roo" are String literals, ie, String object in the literal pool, and so will never be GC'd. However, the concatenation is NOT a literal. String concatenation is performed using a StringBuffer.
SCJP2. Please Indent your code using UBB Code
Originally posted by Graeme Brown:
No. Stringbuffer is only used if one of the Objects to be concatenated is on the heap. In the case of "Kanga" + "Roo" both of these are in the literal pool so the resulting concatenated String can be evaluated at compile time and also placed in the literal pool.
Rob
SCJP 1.4
<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Rob
SCJP 1.4
Originally posted by Rob Ross:
But yes, they all are eligible for gc when the loop ends.
Originally posted by Rajinder Yadav:
Rob, I beg to differ, a statement like
String S = "Good" + "bye";
will creates 3 string in the runtime constant pool, namely: Good, bye, Goodbye
[ February 28, 2002: Message edited by: Rajinder Yadav ]
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |