What all do we actually need to know about String, String Buffer and String Builder API's for SCJP 6.0. Can somebody explain the differences in detail?
I am not sure about in context to the SCJP but following are the major difference
String are Immutable objects which means they are final instances and value can not be changed.
StringBuffer and StringBuilder are better choises to String objects if the you have to make a string on the run. i.e. you have to concatenate
various string objects dynamically.
The exam objectives are listed here Discuss the differences between the String, StringBuilder, and StringBuffer classes.
As said above, you'll have to understand the concept of mutability/immutability. StringBuilder and StringBuffer are pretty much exactly the same exception StringBuilder's methods are synchronized.
Understand the basic API methods. That you can not compare string using == and need to use .equals(). The concept of the String pool.
I would like to suggest to take care of String Pool memory. That will help you in understanding how JVM utilizes String objects and you will be able to know when String objects are created.
When you will be able to understand String pool memory, understanding StringBuffer and StringBuilder is very easy.
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.
-kolli
Stephen Davies
Ranch Hand
Joined: Jul 23, 2008
Posts: 352
posted
0
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: