why is doing the following so bad?
String val = null;
String someOtherValue = "hello"
val += someOtherValue;
It must be pretty bad, but why is that? I had this line in my program and it slowed everything down immensely!
I’m assuming it’s because it keeps re-creating the string? Is this the only reason though?
That exact code is perfectly fine; the compiler will optimize it away.
Doing that in a loop can be slow, since that creates a separate (immutable)
stringobject for each concatenation.Instead, use
StringBuilder.