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

What is the diff b/w string & String buffer?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am a newbie to java programming.

I want to know the difference between using String & StringBuffer classes.

I read that String is fixed length character sequence,whereas StringBuffer is varied length character sequence!

plz explain!

cheers!
 
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

I read that String is fixed length character sequence,whereas StringBuffer is varied length character sequence!



Well, rather than saying "fixed length", we say immutable. Which means that once you've set its value, you cannot change it anymore.

String mystring = "a";
mystring = mystring + "b";

You think that you are using the same String variable, but in fact, JVM create a new one. You'll usually use StringBuffer to concatenate Strings. This avoids creating of new strings each time.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an extract from the javadoc over String class. - Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"You think that you are using the same String variable, but in fact, JVM create a new one."

replace variable with object
 
reply
    Bookmark Topic Watch Topic
  • New Topic