File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Buffer" Watch "String Buffer" New topic
Author

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
Rancher

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?
Dave.
[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: http://aspose.com/file-tools
 
subject: String Buffer