• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings and StringBuffers

 
Ranch Hand
Posts: 33
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anyone outline the differences between String and StringBuffer.

Is there any specific difference in terms of synchronization (acquiring monitor and locks) for these two classes?

Thanks & Regards,

Harish.V
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are immutable. StringBuffers are mutable.
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think there is any difference in terms of acquiring monitors and locks. Why are you asking that?

RK
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the StringBuffer API:

String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.


So StringBuffer methods are synchronized internally. The user doesn't have to worry about this, it just happens. Much like for Vector and Hashtable - and just as for those classes, there are still cases where you might need a synchronized block to span multiple method calls. Don't let "it's synchronized" give ytou too much false security.

Strings, on the other hand, are not synchronized at all. But since they're immutable, it doesn't matter. You can access an immutable instance without synchronization, no problem. (Though if the reference variable you're using is not final, then you might need to sync to avoid problems there, just like for any variable.)

Note that in JDK 1.5, there's a new class StringBuilder. This is just like StringBuffer except it's not synchronized. So it will be faster in cases where you're only accessing the builder with one thread (as is usually the case). If synchronization is needed, I often find it better to explicitly use sync blocks anyway.
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, where do you find out about the new classes in 1.5? I know how to find out about stuff like Generics and Autoboxing, etc. Thx for the 411.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, JDK 1.5 is out in Beta 2 release now, so you can download it and check it out yourself. Here are several good links for learning what's different:

http://java.sun.com/developer/technicalArticles/releases/j2se15/
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html
http://java.sun.com/j2se/1.5.0/docs/api/
 
Harish Vembu
Ranch Hand
Posts: 33
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies and interest. Special Thanks for Jim Yangst- So you are the second mouse here in this forum :-)

Regards,

Harish Vembu
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic