• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Can we replace StringBuilder where we used StringBuffer in JDK1.5

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

In our project we want to migrate from JDK1.4 TO JDK1.5.

We are using StringBuffer in our project.When we migrate JDK1.5 can we replace StringBuilder where we have used StringBuffer(For all the senario) for the performance?

Please suggest.

Thanks
Sumanta
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer has been retrofitted to extend class java.lang.AbstractStringBuilder (which is not public). Almost all methods actually just call the matching overridden method (e.g. append calls super.append). StringBuilder also extends AbstractStringBuilder, so it inherits those methods as well.

Now there may be a single method that isn't available in StringBuilder (but I doubt there is), so from the compiler's point of view you can safely replace StringBuffer with StringBuilder. Of course StringBuilder is not synchronized, so if you require that synchronization your code may fail at runtime.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With Java 1.4, the Java compiler will use StringBuffer for all string concats (that use the "+" operator). With Java 5, the java compiler uses StringBuilder. So, a lot of your operations will switch from StringBuffer to StringBuilder automatically (upon recompile).

However, if you use StringBuffer explicitely, the java compiler will not automatically change it for you. The reason is, it doesn't know to what extend you are using it -- if you pass it elsewhere, then use it in a multithreaded format, switching to StringBuilder may break your app. Regardless, I wouldn't worry about it too much. Uncontended lock grabs have been greatly improved since Java 5, so using StringBuffer, single threaded, is probably not noticeable compared to using StringBuilder.

Henry
 
This tiny ad is wafer thin:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic