Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Diiference between String ,StringBuffer & StringBuilder

 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer this
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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 ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Java scholars,
Thanks to all for your guidance.
Cheers Munees
My Blog
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic