File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes StringBuffer and setLength() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "StringBuffer and setLength()" Watch "StringBuffer and setLength()" New topic
Author

StringBuffer and setLength()

Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
Hi folks,
I have a strange (?) problem in underlying code and can't see why:
void setTextTmp(StringBuffer sb)
{
System.out.println("setTextTmp (sb): "+sb.toString());
this.sbtexttmp.setLength(0);
this.sbtexttmp.append(sb.toString());
System.out.println("setTextTmp: "+sbtexttmp.toString());
//this.sbtexttmp = sb ; //new StringBuffer(s);
}
I want to reuse the sbtexttmp StringBuffer, so I put the length to zero and then append the sb StringBuffer.
The problem is that this doesn't seem to work!
After the append the sbtexttmp stringBuffer stays empty .
If I use 'this.sbtexttmp = sb ' then it does work.
Does anyone have any idea ?
Thx,
Stefan
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Are you sure that sb and sbtexttmp are different objects?
- Peter
Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
Peter,
thx, you were right.
Regards,
Stefan
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: StringBuffer and setLength()
 
Similar Threads
Line Feed Carriage Return problem
a simple counter 1 to 100
Avoiding hardcoded SQL statements
Again String.
String and String Buffer