• 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

String Vs String Buffer

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is it that String immutable & String buffer mutable???
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shiva, String immutable means if we create an objec of string with some value, the value is final we cannot change its value.

where as for string buffer, we can change the value present in string buffer object.
like StringBuffer b=new StringBuffer("kiran");
b.append("kumar");
now b will has kirankumar, we are able to change the content of
stringbuffer but where as for string, String str=new String("kiran");
str will always has content "kiran" even after this method call str.replace('i','r'); not only replace ,it is same for any other method .
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shiva,
Click here for a fantastic article, which explains all you need to know...
 
shiva .B krishna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kiran thanks for the reply. The explanation u gave at mutable & immutable is fine But then i was asked in an interview why is String made immutable & String buffer mutable.So pls try to answer that "why"
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Siva,

String class is generally used when we require constant strings, StringBuffer is generally used if we want frequent operations(like append,delete etc) on the string.
may be they have done for compiler optimization, because there is no need to create a new object if we use constant strings.
StringBuffer was created because if we use strings which change frequently everytime a new object is created if we perform any operation on it.
May be experts like moderators of this form can answer this.
 
I can't beleive you just said that. Now I need to calm down with 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