StringBuilder
Programming languages that have immutable strings (which includes most OO languages)
may have the concept of a StringBuilder - a utility (class) that constructs a single string from many fragments of strings.
...because consider the naive way of doing that is a concatentation like "a"+"b"+"c"+"d"+'e"+"f"
- since such string operations are boolean operations, every concatenaton of two creates a new string object.
- so you create a bunch of temporary objects to be created in memory, almost all of which could be immediately removed (four in that example)
This results in more memory overhead than necessary (potentially on the order of eventual_length**2), and more CPU overhead, both in creation and in cleanup.
If your plan was always 'join list into single thing', then StringBuilder is the thing that lets you collect the parts, and then join them once.
That said, StringBuilder is also sometimes overstated.
When concatenating together just a handful of literals or variables, and it's not in an inner loop, there is often no discernible difference, and there are even cases where StringBuilder may be more wasteful (there is often a bunch of bookkeeping. Also, sometimes a JiT compiler is smart enough to optimize especially the few-literal case, that can't apply to StringBuilders), so this may be micro-optimization and you don't really need to worry about.
Factors include:
- the amount of strings/concatenations
- size of the eventual string
- your instantiation/use
- The amount of work the compiler/interpreter has to do with a StringBuilder that it doesn't for a basic string.
- StringBuilder's implementation
- whether you can then use the StringBuffer itself, or have to create a new immutable String from it again
- The cases that the code has to handle (joining two arguments? Joining an arbitrarily-sized list of strings)
Note that in your language there may be cases where a third option is better. For example, joining a list of strings into a new basic string may well be a an optimized case, certainly more efficient than piece-wise joining, but possibly also better than StringBuilder.