please tell me that is I'm right that when we change in String it create new object but for StringBuffer it keeps old object. please explan me!
Thanks, Vivek Mishra
David Mace
Ranch Hand
Joined: May 26, 2004
Posts: 35
posted
0
Strings are read-only and immutable meaning that the value cannot be changed. When you change the value of the string as you have in the first example, you are making a new object.
Stringbuffers are faster than String objects and should be used when performing concats, etc. Traditionally a string object would be appended through something like:
String x = "xyz"; x += "abc";
Doing the same with a Stringbuffer would look something like:
String x = "xyz"; x.append("abc");
Hope that helps, David
Dad always thought laughter was the best medicine, which I guess is why several of us died of tuberculosis. -Jack Handy
Steven Bell
Ranch Hand
Joined: Dec 29, 2004
Posts: 1071
posted
0
Originally posted by Mishra Vivek: Hi All, please explan difference between String and StringBuffer of the following code.