• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

What Are the Differences Between String and StringBuffer

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both String and StringBuffer are final classes. StringBuffer grows in size dynamically. Could people help to detail other differences between String and StringBuffer?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value in a StringBuffer can be changed/altered by calls to its methods (append, insert etc.), whereas a String's value can never change.
Appending to a StringBuffer is a lot more efficient that adding multiple String together.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the main difference is that String is immutable and StringBuffer is mutable.

Kaps
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects of type String are read only and immutable. When u try to change a string object what actually happens is that a new strign is created.
The StringBuffer class is used to represent characters that can be modified.

The performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can go through the following link to understand the difference better.

http://www.whizlabs.com/tutorial/scjp/j-scjp-9-2.html

I am sure it would help you.
[ July 09, 2004: Message edited by: natarajan raman ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Respected Friend,
Herbert Schildt in his book "The Complete Reference Java 2" explined the difference as follows :

"StringBuffer is a peer class of String that provides much more functionality of strings.As you know String represents fixed length,immutable characters sequences.In contrast,StringBuffer represents growable and writeable characters sequences.StringBuffer may have characters and sub strings inserted in the middle or appended to the end.StringBuffer will automatically grow to make room for such additions and often have more characters pre allocated than are actually needed,to allow room for growth.Java uses both classes heavily,but many programmers deal only with String and let Java manipulate StringBuffers behind the scenes by using the overloaded + operator".

Taqi Raza
[email protected]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic