This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String Buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Buffer" Watch "String Buffer" New topic

String Buffer

Argm Mastoi
Ranch Hand

Joined: Oct 16, 2001
Posts: 35
Can any one simply define "what String Buffer is used for? and bit of its characteristics"
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
the String Buffer class creates "mutable" objects, meaning that you can alter the characters inside without having to create a completely new object. I use them only as a memory saving device. I'm sure there's other reasons to use them however. Someone please add to my reply?
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud

Michael J Bruesch<br /><i>I code, therefore I am.</i>
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Regular strings are much slower as everytime you look at them they go off and make a copy of themselfs. So if speed is a consideration you might want to use StringBuffer instead of String.

Please ignore post, I have no idea what I am talking about.
Argm Mastoi
Ranch Hand

Joined: Oct 16, 2001
Posts: 35
Thanx for your replies but i'm still not having a clear idea for the usage of string buffer
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The problem with Strings, as Michael implied, is that they are immutable. This means that if you change the value of a String, you actually create a new String object and no longer refer to the old object. I believe the main reason for String immutability is that it allows the JVM to do a bunch of tricks to optimise code.
It does cause a few problems tho

Normally you'd expect the altered version of the String to be printed out, but it is infact the old version, since the reference to the object is changed.
Now we have the next bit of code, concatenating an array of Strings into a single String:

The problem here is that every time you alter the value of resultString, you get a new (short lived) object.
ie, the previous code is equivalent to this:

If you did this a lot and had large String arrays, you would end up paying a large price for wasted object creation and having to garbage collect a lot of wasted String objects.
This is where the StringBuffer comes in, It provides a class that is not a String but it does optimise for the inefficient operations in the String class so that this is a lot more efficient:

(note that there are still considerations such as StringBuffer lengths etc and the fact that compilers can do some String to StringBuffer conversions of there own, but it's a start)
Is this better?
[This message has been edited by David O'Meara (edited October 30, 2001).]
Argm Mastoi
Ranch Hand

Joined: Oct 16, 2001
Posts: 35
cool, u guys are marvelloue, thanx
I agree. Here's the link:
subject: String Buffer
It's not a secret anymore!