user2287359
user2287359

Reputation: 509

What is the proper way to completely replace a StringBuilder?

Looking for the best (?) way to replace contents of a StringBuilder

I normally use this

StringBuilder stringBuilder = new StringBuilder("abc");

stringBuilder.setLength(0);
stringBuilder.append("12");

I guess one could also point to a new StringBuilder

StringBuilder stringBuilder = new StringBuilder("abc");

stringBuilder = new StringBuilder("12");

Upvotes: 0

Views: 83

Answers (1)

Erik Pragt
Erik Pragt

Reputation: 14617

Reusing a StringBuilder like this usually saves you little to nothing. Personally, I would never bother with these things; just create a new StringBuilder object, it's simple, consistent (you're not resetting your POJO's, right?).

So, I'd say, keep it simple, and go for the second option, unless you've got some interesting constraints not mentioned in your initial post.

Upvotes: 3

Related Questions