RFR (XS) 8221307 : String.substring() OOB exception on start index reports improper information

Ivan Gerasimov ivan.gerasimov at oracle.com
Thu Aug 8 18:47:04 UTC 2019

Hi Claes!

On 8/8/19 6:56 AM, Claes Redestad wrote:
> Hi,
> perhaps a stupid question, but why isn't String::substring(int) 
> calling substring(beginIndex, length())? That'd ensure consistent error
> messages, but otherwise preserve semantics.
It's a good point, actually!

I suspect that originally substring(int) had a separate implementation 
for performance reason.

I agree, it would make most sense to implement one via another unless 
there's a performance penalty.

Let me do some measurements, and then I'll report back.

With kind regards,


> /Claes
> On 2019-08-08 02:17, Ivan Gerasimov wrote:
>> Hello!
>> The exception thrown by substring(int) may look confusing.
>> For example it produces "String index out of range: -1" when the 
>> index is length+1.
>> It is proposed to make the error message more clear, similar to what 
>> we have for substring(int, int).
>> BUGURL: https://bugs.openjdk.java.net/browse/JDK-8221307
>> WEBREV: http://cr.openjdk.java.net/~igerasim/8221307/00/webrev/
>> Would you please help review?
With kind regards,
Ivan Gerasimov

More information about the core-libs-dev mailing list