RFR: 8266972: Use String.concat() in j.l.Class where invokedynamic-based String concatenation is not available

Сергей Цыпанов sergei.tsypanov at yandex.ru
Thu May 13 06:19:55 UTC 2021


> 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


More information about the core-libs-dev mailing list