aspose file tools*
The moose likes Beginning Java and the fly likes Why should we use StringBuilder? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why should we use StringBuilder?" Watch "Why should we use StringBuilder?" New topic
Author

Why should we use StringBuilder?

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear all,
When I was encountering a post on string formatting, somebody said that it's better to use StringBuilder other than StringBuffer.
Why so & what are the benefits of using so?
Regards.
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
Have a look at the javadocs for StringBuilder (java1.5 and later). It gives an explanation as to why in most cases StringBuilder is preferred.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
That's alright.
But I want programmers experience on using stringbuilder instead of stringbuffer or string.
Regards.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

who do you think wrote the javadocs but programmers? The class was probably created because of feedback from programmers.

One of our goals here is to teach people to be better programmers, and not just hand them the answers. One of the ways to become a better programmer is to learn to read the documentation. If, after reading it, you have specific questions by all means post them. But simply saying "i don't want to read the docs, just tell me the answer" (which is how your post reads to me) is really not going to get you much around here.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Great thoughts Fred & I respect your opinions
Regards
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
with due respect to Fred, going through the javadocs for stringbuilder presented in :
StringBuilder javadocs

The following aspects are noted:

1) We can use StringBuffer or StringBuilder as long as these instances are used by only one thread.
But in this case it is better to use StringBuilder as it is quick to implement & also it has overloaded append & insert methods.

2) But when we want to synchronize access to instance, then we can use StringBuffer as it is implicitly synchronized i.e out of multiple threads trying to access that instance , only one thread will be able to access that instance.

3) StringBuilder has overloaded append & insert methods.

Right! Any updates!
Regards
[ December 10, 2008: Message edited by: Campbell Ritchie ]
vaibhav mishra
Ranch Hand

Joined: Jun 18, 2008
Posts: 168
long story short,
stringbuffer is a synchronized class implementation and stringbuilder is not
both of them behave same but StringBuffer provide thread safety, but just for it's methods, which result in a small performance hit , which doesn't matter for small App but is fairly considerable where lots of objects are involved so it's better to use stringbuilder because it is fast, if you are dying for the thread safety feature(as -if you're using threads ) then stringbuffer provides some additional thread safety,

StringBuilder is new and is faster then StringBuffer, that's the reason you should prefer it.

consider this article which shows almost 34% difference in terms of performance.

[ December 10, 2008: Message edited by: vaibhav mishra ]
[ December 10, 2008: Message edited by: Campbell Ritchie ]

SCJP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
I am sorry; I didn't check who was posting, and I thought you were giving answers when you ought not to. So I deleted the answers, then realised that you had done what Fred suggested. Well done finding the details.

I am very sorry about that mistake.

I have, fortunately, been able to restore the original text of your posts, plus an "edited by Campbell Ritchie."

By the way: it is probably acceptable to say "with due respect" in India, or the USA, but in England it is considered very rude.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Originally posted by vaibhav mishra:
consider this article


That is a nice little demonstration. And it is explained simply so you can try it on your own computer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why should we use StringBuilder?
 
Similar Threads
String and StringBuffer
StringBuffer & StringBuilder and equals()
Regarding StringBuffers and StringBuilders
How to make String Class Mutable
String's insert