Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer and setLength (part 2)

 
Stefan Geelen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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];
 
Stefan Geelen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stefan Geelen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic