• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why should we use StringBuilder?

 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's alright.
But I want programmers experience on using stringbuilder instead of stringbuffer or string.
Regards.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great thoughts Fred & I respect your opinions
Regards
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic