• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringBuffer and setLength()

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that sb and sbtexttmp are different objects?
- Peter
 
Stefan Geelen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,
thx, you were right.
Regards,
Stefan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic