> Before we start to change every usage of String+String into String.concat,
> shouldn't the compiler be so smart to do that for us?
> Currently it compiles to invokedynamic if available and to using
> StringBuilder otherwise. Now why doesn't it compile to String.concat instead
> of StringBuilder for the case when invokedynamic is not available as target?
I think the main reason is that
String str = "smth is " + null;
returns "smth is null" and with current implementation of String.concat()
the same expression throws NPE. See the code of String.concat():
public String concat(String str) {
if (str.isEmpty()) {
return this;
}
return StringConcatHelper.simpleConcat(this, str);
}
Regards,
Sergey