In Sample 1 here : on Line 3 there will be no object that is Gc'ed where as in line 3 of Sample 2 there is One object that is Gc'ed/ Reason : In Sample 1 the string created is kept in the JVM pool - and hence not destroyed when referances to it are removed.
I Think this is true that String Objects created in pool are never Garbage Collected, Whereas String Objects created by new keyword are created on heap and are Garbage Collected.
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
This is an interesting topic and at the same time it has the distinction of NOT being on the exam. In their infinite wisdom, the exam creators graciously decided not to mix String objects in with garbage collection questions.
So, when you're studying up for GC questions, don't worry about the string constant pool! GC questions will use other kinds of objects!
hth,
Bert
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?