To make a long story short, why does Java not throw an IndexOutOfBoundsException for this line of code?
str.substring(str.length())
Is it just by definition of IndexOutOfBoundsException? To make start/end indices take the same range of values for programming convenience/symmetry? Is it just a decision by Oracle? Is there just a special case under the hood that handles this case with the inclusive start index? Or is there some underlying reason...
I read the documentation and they say it just returns the empty string (""). But I'm wondering if this is something to worry about. Will it ever change? I'm thinking no, but I'd like to hear it from someone else. I have some code that depends on a line like this to work because I use substring and indexOf(...)+1 to split some lines up, and I really don't want to put unnecessary logic around the line of code.
The substring() method returns a substring of the given string. This is a built-in method of string class that can be called by a string, it returns the substring based on the index values passed to this method. For example: “Beginnersbook”. substring(9) would return “book” as a substring.
The substring() method returns a substring from the given string. The substring begins with the character at the startIndex and extends to the character at index endIndex - 1 . If the endIndex is not passed, the substring begins with the character at the specified index and extends to the end of the string.
When we take substring from original string, new String object will be created in constant pool or in heap. The value[] char array will be shared among two String objects, but count and offset attributes of String object will vary according to substring length and starting index.
Java String class provides the built-in substring() method that extract a substring from the given string by using the index values passed as an argument. In case of substring() method startIndex is inclusive and endIndex is exclusive.
Here´s the docu of the substring(beginindex)
javadoc.
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
Examples:
"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)
Parameters: beginIndex the beginning index, inclusive.
Returns: the specified substring.
Throws: IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
Basicly because it just throws if beginindex > length
and will return an empty String
if beginindex == length
.
And as @Kayaman pointed out, Oracle wont destroy the backward compatibility to other java version just to change this.
You said you already read the docs, so you know it returns empty string because that's what the docs say it will do. But the underlying reason is as follows:
For the purposes of substring, the indices of a string are interpreted as being between the characters in the string. So index 0 is before the first character, index 1 is between the first and second, and index str.length() is after the last character. The substring starting right after the last character is "".
It makes more sense in the two-argument version of substring, where having it defined this way saves you from writing special case logic when pulling out a substring that may or may not include the last character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With