Reputation: 509
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
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