Search code examples
javastringstringbuilder

Can one StringBuilder be used for constructing multiple strings in Java?


I'm trying to create a certain amount of long Strings. Should I create a new StringBuilder for each new String or can one create multiple?

I need to create 4 strings s1, s2, s3, s4. Can they all be created as separate strings by one StringBuilder, or do I have to create a new StringBuilder for each s?


Solution

  • Yes, a single StringBuilder can be used to build multiple independent Strings