aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String, String Buffer and String Builder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String, String Buffer and String Builder" Watch "String, String Buffer and String Builder" New topic
Author

String, String Buffer and String Builder

atif aslam
Greenhorn

Joined: Sep 17, 2008
Posts: 12
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

Joined: Jun 20, 2008
Posts: 7
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

Joined: Jan 30, 2009
Posts: 27
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

Joined: Jun 20, 2008
Posts: 7
exception StringBuilder's methods are synchronized.


It's StringBuffer whose methods are synchronized.
Pankaj Upadhyay
Greenhorn

Joined: Nov 06, 2008
Posts: 21
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.


Pankaj Upadhyay (SCJP 1.6 == 86%)
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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()!
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String, String Buffer and String Builder
 
Similar Threads
doubt in thread
wat's d diff b/w string buffer and string builder
string,string buffer,string builder
when toString() is implicitly called
String and null