• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Diiference between String ,StringBuffer & StringBuilder

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer this
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Java scholars,
Thanks to all for your guidance.
Cheers Munees
My Blog
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic