8/25/2006

StringBuilder vs StringBuffer

StringBuilder was introduced in JDK 1.5. What's the difference between StringBuilder and StringBuffer? According to javadoc, StringBuilder is designed as a replacement for StringBuffer in single-threaded usage. Their key differences in simple term:

  • StringBuffer is designed to be thread-safe and all public methods in StringBuffer are synchronized. StringBuilder does not handle thread-safety issue and none of its methods is synchronized.

  • StringBuilder has better performance than StringBuffer under most circumstances.

  • Use the new StringBuilder wherever possible.
Other than that, the two classes are remarkably similar with compatible API. It seems the author just copied StringBuffer.java to StringBuilder.java, removing all occurrences of "synchronized". Here is a little trace left in StringBuilder.readObject method:
/**
* readObject is called to restore the state of the
* StringBuffer from a stream.
*/
private void readObject(java.io.ObjectInputStream s)
Note the above javadoc still refers to StringBuffer where it should be StringBuilder.
2 interesting methods in StringBuilder or StringBuffer are reverse() and equals(Object):
  • reverse() method modifies the current this StringBuilder/StringBuffer, and also returns itself. More details in Reverse a String with StringBuilder or StringBuffer

  • equals(Object) method is not implemented (overridden) in StringBuilder/StringBuffer. So comparing 2 instances of the same content will return false. Also there is no equalsIgnoreCase() method on StringBuilder/StringBuffer. However, toString() is implemented to return a string representing the data in this StringBuilder/StringBuffer.

19 comments:

Shoeb said...

Really funny....

Jayaprabhakar said...

I dont agree with, use the StringBuilder whereever possible. I have shown an example here.

Java Tutorials: ArrayList or Vector?

The same example will apply for StringBuilder vs StringBuffer also.

tpjava said...

I have always used StringBuffer but was lampooned the other day in a Java forum for using this rather than StringBuilder.
I think if sync issues are a real posibility then I will stick with StringBuffer, esp for the critical financial systems we are building here otherwise I will use Stringbuilder.

Mani said...

i can't see any method called readObject in StringBuilder class :-)

Anonymous said...

It's a private method.

Anonymous said...

Hi all, I have a static method can I use local String builder object in static method. Is there any thread safty issue?

Prageeth said...

Thanks for the simple informative post.

Vinod Kumar Kayartaya said...

@Mani (i can't see any method called readObject in StringBuilder class)

Both StringBuffer and StringBuilder extend from AbstractStringBuilder, and the method readObject is in that.

http://javatrainer.org

Cristian said...

@Anonymous (using a local string builder): Yes, it should be fine, since the variable is local to the method it will not be shared between threads; each thread will get its own copy on its stack. StringBuilder is the recommended class under such circumstances.

Javin Paul said...

Yes main difference being Stringbuilder not synchronized so a faster solution than StringBuffer if you want to do string operation. you may find my post String is immutable in Java useful as well

Anonymous said...

Thanks for the Information....

PrasadHejamady said...

Thanks for the information..

Anonymous said...

StringBuffer is really becomes bottleneck if you do two much string manipulation on a concurrent application but StringBuilder is not a choice there due to its inherent capacity and ultimately we ended up using plain old String vs StringBuffer vs StringBuilder

Anonymous said...

Someone mentioned both classes extend AbstractStringBuilder. This is true. However, this method (shown in the code) is not in the class see here:

http://kickjava.com/src/java/lang/AbstractStringBuilder.java.htm

Its also not in interface appendable, or CharSequence ( the others involved).
Java is now open source and you have always been able to view the code of classes. Its a good idea to do so just google java source code and you can find anything you'd like.

This was definitely copy and pasted and why not? The class is literally the same but is more efficient in non-thread based environments because its not doing any of the work to keep it thread safe

Anonymous said...

Nice Post. I've also jot down points of difference between String, StringBuffer and StringBuilder. Click below to read
Difference between String, StringBuffer and StringBuilder in Java

Radhakrishnan said...

Please visit http://yourjavatutor.blogspot.in/2011/12/difference-between-stringstringbuffer.html for more details.

Anonymous said...

http://ilovemeturtle.blogspot.com/2012/06/string-vs-stringbuilder-vs-stringbuffer.html

Miha said...

Use stringbuilder

JB said...

Thought I'd share this, manually building Stringbuilders is such a pain:

www.buildmystring.com