• 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

Difference between String and StringBuffer

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can anyone guide me, what is exact difference between String and StringBuffer. I know that String is immutable and StringBuffer is mutable.
But i couldn't understand where we use String and StringBuffer.
Please try to explain me in detail if possible.

Thanks & Regards,
Vivek Mishra
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are planning on building a string value one piece at a time by appending new values to it, StringBuffer is a better option than String. StringBuffer's append() method actually appends the new part to the existing value. You can "append" to a String, but actually what you are doing is throwing the old value away in favor of the new one. Garbage collection will tidy this up for you in the long run, but with StringBuffer you are not relying on that.

At least that's the way it was explained to me.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're doing lots of String I/O, moving values, etc. StringBuffer is more efficient.

Think of the situation where you have to compare or process 100,000 records of data. The object StringBuffer would immediately either go out of scope, or be reassigned a new variable reference. In either case, you're allowing for "cleaner" garbage collection.

The way I understand it, especially if you instantiate string outside of your loop, it could take the garbage collector longer to get to the String objects.

Hope this helps!

Marcus
 
Saurabh Saha
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave and Marcus for giving reply,
Sorry I couldn't understand much. What I got is, when we need to do lots of String I/O we use StringBuffer otherwise use String.
But what is difference between both still not much clear.
Please explane me by simple program examples.

Thanks & Regards,
Vivek Mishra
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mishra,

The main difference is :

* String CAN'T be modified
* StringBuffer CAN be modified

You may look at the reasons here why two string classes

Best regards,
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vivek
got to the following link ..this may serve all ure queries about String and StringBuffer..
http://www.precisejava.com/javaperf/j2se/StringAndStringBuffer.htm

Kunal
 
Saurabh Saha
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kunal,
Thanks for a good link.
This link really solve my all queries about String and StringBuffer.

Regards,
Vivek Mishra
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
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