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.