• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

String, String Buffer and String Builder

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

exception StringBuilder's methods are synchronized.



It's StringBuffer whose methods are synchronized.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic