Indeed StringBuffer is Thread Safe in that its methods are synchronized. However remember using Thread Safe classes is all good and well, but don't rely on them alone for thread safety, make your methods using them synchronized if they can alter the state of your Objects! Further synchronization causes a little more overhead, as the threads are locking and blocking, so if you need to keep your memory footprints down and speed up, consider if you need StringBuffer, or could you use other threading techniques.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
one of the most common question that can asked is related to the number of objects at a particular time in a given piece of code.
Joined: Jul 23, 2008
StringBuilder and Buffer are identical, However, performance-wise StringBuilder is faster as it it's methods are not synchronized. Also, in comparisson with string literals and String objects, the destructive methods do not require the creation of a new String object on the heap:
-- However if we now look at the StringBuffer / StringBuilder class --
String pooling is used in the creation of String literals, so if I state:
From this we see three String Objects, and 4 references.