| Author |
Diiference between String ,StringBuffer & StringBuilder
|
Muneeswaran Balasubramanian
Ranch Hand
Joined: Mar 19, 2010
Posts: 138
|
|
Hi Java Scholars,
I always confuse about the difference between string,stringBuffer&StringBuilder.Please any one explain the differences between them and where we have to use them?
Cheers Munees
|
Cheers Munees
My Blog
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12928
|
|
String is immutable. The content of a String object never changes after it has been created.
StringBuffer and StringBuilder represent mutable sequence of characters.
StringBuffer and StringBuilder have the same function, but StringBuilder is newer and should be preferred. The difference between the two is that most of the methods of StringBuffer are synchronized, while the methods of StringBuilder are not. Synchronization is only needed when you use the same object in multiple threads at the same time, which you don't do most of the time. Since synchronization adds some runtime overhead, doing it all the time is a waste, so the Sun people added StringBuilder without the unnecessary synchronization. (This is similar to the difference between Vector and ArrayList).
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
PrasannaKumar Sathiyanantham
Ranch Hand
Joined: Nov 12, 2009
Posts: 110
|
|
|
Please refer this
|
To err is human,
To forgive is not company policy
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
(Jesper already replied while I was typing )
A String is a String. No problem with that, right ? A minor concern with Strings is that they are immutable. When you append another String to it, a new String is being instantiated. For example:
Here, you could think that the "a" instance is the same after the concatenation, but no. Strings are immutable, so the "a" instance will not change. When we append something to it, a new instance will be created and assigned to "a" again. This is time consuming (very slightly...). To prevent that, StringBuffer came to the rescue. You can append Strings and do other String operations without having new instantiation all over. StringBuffer is thread-safe though, which means that it has some special protection to be multi thread friendly. This may also be a bit time consuming (slightly...) So then came StringBuilder, a not thread-safe StringBuffer. Doing the same stuff, but not thread-safe. Who cares if you're single threaded ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12928
|
|
Note also that if you use + (or +=) with String objects, the compiler does some magic - it generates code that uses a StringBuilder behind the scenes to concatenate strings. Have a look at this example:
Compile it and then disassemble it with the command: javap -c Example and you'll see this:
Translating this back to regular Java, you see that it looks something like this:
|
 |
Sriram Sharma
Ranch Hand
Joined: Apr 12, 2006
Posts: 89
|
|
So, the total number of string objects created in this process is three.
They are...
hello
world
hello world
Am I right???
Regards,
Sriram
|
 |
Muneeswaran Balasubramanian
Ranch Hand
Joined: Mar 19, 2010
Posts: 138
|
|
Hi Java scholars,
Thanks to all for your guidance.
Cheers Munees
My Blog
|
 |
 |
|
|
subject: Diiference between String ,StringBuffer & StringBuilder
|
|
|