This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes StringBuffer and setLength (part 2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "StringBuffer and setLength (part 2)" Watch "StringBuffer and setLength (part 2)" New topic
Author

StringBuffer and setLength (part 2)

Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
Hi,
I have another question:
Suppose a stringBuffer has a lenght of 100 characters.
If I set the length to 0 (using setlenght(0)) does this mean that the underlying array is discarded and a new array is made of 0 bytes (or the default 16), or is the array kept and are all characters replaced by \u0000 ?
I ask this because I would like to reuse a StringBuffer, but if possible without discarding the array (to save time) beneath.
Stefan
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
From the source

// If newLength is zero, assume the StringBuffer is being
// stripped for reuse; Make new buffer of default size
value = new char[16];


"JavaRanch, where the deer and the Certified play" - David O'Meara
Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
Hi,
thx for the response. I also saw that in the source, but I also saw:
CODE]
if (shared) {
if (newLength > 0) {
copy();
} else {
// If newLength is zero, assume the StringBuffer is being
// stripped for reuse; Make new buffer of default size
value = new char[16];
shared = false;
}
} [/CODE]
what about share ?
Stefan
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Reusing a StringBuffer is not as useful as you might think. Any time you call toString() and then make a change to the StringBuffer contents after, the internal char[] array will be replaced for you (whether you want to or not). This is because the StringBuffer has optimized the toString() operation by reusing the StringBuffer's internal char[] array as the internal char[] array for the new String as well. This makes the new String creation very fast. But since the String must be immutable, StringBuffer can't make any further changes to that array. So the moment you try to make further changes, it will create a new internal array with the contents of the old. (That's what the variable "shared" is for - monitoring whether the char array is being used by an outside String or not.) This operation would also be the main cost of creating a whole new StringBuffer, if you were to do that. So in most cases it's cleaner to just create a new StringBuffer rather than reuse an old one.
BTW, when I look at the Java source I don't see the same stuff you guys do. Are you looking at JDK 1.4.1, or an older version?


"I'm not back." - Bill Harding, Twister
Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
Hi,
thx for the response. I looked in the src.jar coming with the jdk1.3.1_07 download.
So if I understand well, there is no way I can reuse a StringBuffer but keeping the length ? I might empty the StringBuffer using the delete() method but if a .toString() method has been used previously first a copy of the internal array will be made and then the characters are deleted.
Right ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer and setLength (part 2)
 
Similar Threads
StringBuffer capacity
String and StringBuffer (internal structure)
SAX parsing
String and StringBuffer
Why initial size of String Buffer is 16