File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings and StringBuffers

 
Harish Vembu
Ranch Hand
Posts: 33
Java MySQL Database Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable. StringBuffers are mutable.
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java MySQL Database Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic