Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String, String Buffer and String Builder

 
atif aslam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Amit Singla
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

StringBuffer is thread safe but StringBuilder is not thread safe. So if you have a single thread environment, StringBuilder will give you better
performance over StringBuffer.
 
Kieren Dixon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amit Singla
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exception StringBuilder's methods are synchronized.


It's StringBuffer whose methods are synchronized.
 
Pankaj Upadhyay
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Hope I am clear.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

For example


-- 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic