This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes StringBuffer/append/toString Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringBuffer/append/toString Question" Watch "StringBuffer/append/toString Question" New topic
Author

StringBuffer/append/toString Question

Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
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

Joined: Nov 06, 2000
Posts: 1510
The API is correct. You are appending a String that is stored in a StringBuffer.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Oct 01, 2000
Posts: 167
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

Joined: Mar 07, 2002
Posts: 1427
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 ]

Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer/append/toString Question