Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Why is str.substring(str.length()) an acceptable line of code? [closed]

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.

like image 800
Ryan Conrad Avatar asked Nov 05 '15 14:11

Ryan Conrad


People also ask

What is the purpose of the substring () method and how do you use it?

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.

What does str substring do in Java?

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.

Does substring change the original string Java?

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.

Is substring in Java inclusive?

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.


2 Answers

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.

like image 138
SomeJavaGuy Avatar answered Nov 15 '22 10:11

SomeJavaGuy


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.

like image 44
gatkin Avatar answered Nov 15 '22 12:11

gatkin