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.
/**Note the above javadoc still refers to StringBuffer where it should be StringBuilder.
* readObject is called to restore the state of the
* StringBuffer from a stream.
*/
private void readObject(java.io.ObjectInputStream s)
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.
Tags:
24 comments:
Really funny....
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.
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.
i can't see any method called readObject in StringBuilder class :-)
It's a private method.
Hi all, I have a static method can I use local String builder object in static method. Is there any thread safty issue?
Thanks for the simple informative post.
@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
@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.
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
Thanks for the Information....
Thanks for the information..
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
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
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
Please visit http://yourjavatutor.blogspot.in/2011/12/difference-between-stringstringbuffer.html for more details.
http://ilovemeturtle.blogspot.com/2012/06/string-vs-stringbuilder-vs-stringbuffer.html
Use stringbuilder
Thought I'd share this, manually building Stringbuilders is such a pain:
www.buildmystring.com
String is an immutable class while StringBuilder & StringBuffer are mutable classes. StringBuffer is synchronized while StringBuilder is not.
Below link can be useful to find more differences between String, StringBuffer & StringBuilder
Difference between String, StringBuffer and StringBuilder
http://www.instanceofjava.com/2015/01/string-vs-stringbuffer-vs-stringbuilder.html
Difference between String, StringBuffer and StringBuilder
Great and Useful Article.
Online Java Training
Java Online Training India
Java Online Course
Java EE course
Java EE training
Best Recommended books for Spring framework
Java Interview Questions
Java Course in Chennai
Java Online Training India
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
Internet Of Things training in electronic city
Post a Comment