View RSS Feed

Java String

String vs. StringBuffer

Rate this Entry
by , 01-08-2012 at 12:26 PM (968 Views)
Following are the major differences between these 2:

• String is immutable as compared to StringBuffer. You need to create a new instance to replace an object.

Java Code: This is an example of immutable string
//Inefficient version using immutable String
String output = “Some text”
Int count = 100;
for(int I =0; i<count; i++) {
output += i;
return output;
• StringBuffer is mutable. Also it is not synchronized.

Java Code: This is an example of mutable string
//More efficient version using mutable StringBuffer
StringBuffer output = new StringBuffer(110);
Output.append(“Some text”);
for(int I =0; i<count; i++) {
return output.toString();

Submit "String vs. StringBuffer" to Facebook Submit "String vs. StringBuffer" to Digg Submit "String vs. StringBuffer" to Submit "String vs. StringBuffer" to StumbleUpon Submit "String vs. StringBuffer" to Google

Updated 01-14-2012 at 07:42 PM by Java String

String , StringBuffer