On Tue, 23 Jul 2024 12:12:41 GMT, Chen Liang <[email protected]> wrote:
>> src/java.base/share/classes/java/lang/String.java line 2991:
>>
>>> 2989: }
>>> 2990: if (isEmpty()) {
>>> 2991: return str;
>>
>> This case should probably be reflected more precisely in the specification,
>> or do `return new String(str);` to avoid changing semantics for this
>> corner-case.
>
> The spec for concat only asked for equal representation instead of the
> existence of a new identity. Thus this should be fine.
Yes, this isn't beholden to JLS 15.18.1, and it's already specified that
`foo.concat("")` returns `foo` - so why shouldn't `"".concat(foo)` return
`foo`? But still it's an observable semantic change so some care needs to be
taken - possibly even a CSR is warranted. Doing `return new String(str)` to
retain behavior avoids that headache for a fringe case.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/20253#discussion_r1687990148