• Post Reply Bookmark Topic Watch Topic
  • New Topic

String to StringBuffer to synchronized  RSS feed

 
hasan khan
Ranch Hand
Posts: 223
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to speed up String concating i am using StringBuffer, but it is synchronized what do i do;
should i go back to '+' operator for String concating or should i use StringBuffer as suggested on this page http://www.protomatter.com/nate/java-optimization/ by using one synchronized block for multiple append()
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using JDK 1.5, use StringBuilder which is not synchronized.
 
hasan khan
Ranch Hand
Posts: 223
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i (my company's website) am still using jdk 1.4.2_08, so what to do
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if you time a StringBuffer versus a StringBuilder you will see very little difference -- uncontested synchronization is cheap. So in 1.4, use a StringBuffer when appropriate and don't sweat the synch.
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff's right. In a web application, your bottlenecks are probably going to be network and database related. Unless you can profile your system and identify string concatenation as a problem, don't worry about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!