• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer/append/toString Question

 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class testStringBuffer {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("sb1");
StringBuffer sb2 = new StringBuffer("sb2");
sb1.append(sb2); /* why does this work */
sb1.append(sb2.toString()); /* this should work, and does */
System.out.println(sb1);
}
}
The API documentation says that you need a String, not a StringBuffer as an argument to append, but a StringBuffer also works. Is the documentation incomplete regarding that possibility.
Thanks for the help.
Rob
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API is correct. You are appending a String that is stored in a StringBuffer.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Levo:
The API documentation says that you need a String, not a StringBuffer as an argument to append, but a StringBuffer also works. Is the documentation incomplete regarding that possibility.

Not only is the API dicumentation not incomplete regarding this condition, it states explicitly that a StringBuffer is a possible parameter for one of the 11 append methods available in the StringBuffer class. In other words, the append method is overloaded (with 11 varaitions) to accept parameters of type:
boolean
char
char[]
char[], int, int
double
float
int
long
Object
String
StringBuffer
Note: The API documentation for StringBuffer
Good Luck.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just noticed that StringBuffer.append(StringBuffer) is "since 1.4" in which case prior versions would do exactly what the API documentation says it would do.
"The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string buffer."
The parameter StringBuffer would be viewed as an Object (invoking StringBuffer.append(Object)), in which case String.valueOf(Object) would be invoked using the parameter Object, which in turn invokes Object.toString() - and thus, in your example, sb2.toString() is effectively appended to sb1.
Make sense?
Good Luck.
[ April 03, 2002: Message edited by: Dirk Schreckmann ]
 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, and Dirk especially for pointing out that prior to 1.4, the append method using a StringBuffer would behave as an Object, thus causing the desired result.
This forum continues to be a tremendous help.

Rob
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, to follow-up, I understand why they need a separate append() function for each primitive type -- they are different.

But if they already have the generic StringBuffer.append(Object) function, why do they need a new StringBuffer.append(StringBuffer) in JDK 1.4?
[ April 03, 2002: Message edited by: Michael Yuan ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I imagine the new method is more efficient. StringBuffers and Strings allow each other a sort of privileged access to their internal char[] buffers, which is faster than creating a new String object for the data before copying it to yet another different object (the other StringBuffer). So if the compiler knows that the Object being copied from is in fact a StringBuffer, it can make use of the more efficient methods.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
I imagine the new method is more efficient.
Indeed it is. I just took a look at the StringBuffer source code and it copies the StringBuffer's internal array onto the end of the current StringBuffer's internal array.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic