As far as I know, StringBuilder helps to reduce memory usage by not creating temporary string instances in the string pool during concats.
But, what happens if I do sth like this:
StringBuilder sb = new StringBuilder("bu");
sb.append("b"+"u");
Does it compile into
sb.append("b");
sb.append("u");
? Or it depends on optimalization flags? Or I loose the whole benefit if stringbuilders?
Or this quetion makes no sense? 🙂
It compiles to
sb.append("bu"), because the compiler translates the concatenation of multiple String litterals to a single String litteral.If you had
it would compile it to
So you should prefer
in this case.