Re: String Vs. StringBuffer
gaurav v bagga wrote:
(call this method A)
method(){
String a="";
String c="";
}
(call this method B)
method(){
StringBuffer a=new StringBuffer();
StringBuffer c=new StringBuffer();;
}
will String a,c get garbage collected after method
what about StringBuffer ?
When (A) has finished, the variables `a` and `b` vanish.
The variables are not garbage collected, but their values
might be. However, "" is a string literal, hence interned,
hence it doesn't go away until the JVM finishes.
When (B) finishes, it's `a` and `b` vanish and their values
become eligible for garbage collection. Since there are no
other references to these StringBuffers, eventually (perhaps
for very small values of "eventually", perhaps for large ones)
they will be reclaimed.
If, in (A), you had say written
String a = new String( "" );
then you would have made a /copy/ of the empty string, and once
this (A) finished, that copy can be reclaimed. (Off-hand I can't
remember whether the copy has a copy of the underlying char[].)
--
Chris "electric hedgehog" Dollin
"The path to the web becomes deeper and wider" - October Project