Java Why is str.substring (str.length ()) a valid line of code?

In short, why doesn't Java throw an IndexOutOfBoundsException for this line of code?

str.substring(str.length()) 

Is it just by the definition of IndexOutOfBoundsException? So that the start and end indices have the same range of values ​​for ease of programming / symmetry? Is this just an Oracle solution? Is there only a special case under the hood that handles this case with the start index turned on? Or is there some main reason ...

I read the documentation and they say that it just returns an empty string (""). But I wonder what to worry about. Will this ever change? I do not think so, but I would like to hear it from someone else. I have code that depends on such a line, because I use a substring and indexOf (...) + 1 to separate some lines, and I really don't want to put unnecessary logic around a line of code.

+5
source share
5 answers

Here is the substring(beginindex) javadoc document.

Returns a new string, which is a substring of this string. The substring begins with the character at the specified index and continues to the end of this line.

Examples:

unlucky .substring (2) returns happy

Harbison .substring (3) returns bison

void .substring (9) returns "" (empty string)

Parameters: beginIndex starting index, inclusive.

Returns: the specified substring.

Throws: IndexOutOfBoundsException - if beginIndex is negative or greater than the length of this String object.

Mostly because it just throws if beginindex > length and returns an empty String if beginindex == length .

And as @Kayaman pointed out, Oracle will not destroy backward compatibility with another version of Java to change this.

+10
source

As explained in java docs , an IndexOutOfBoundsEx exception is thrown only if beginIndex is negative or greater than the length of this String object.

And also check the example below from java docs

 "emptiness".substring(9) returns "" (an empty string) 

As explained in the comment , but this is not the case for the charAt method. This method is correct and accurate according to the documents and from the point of view of the user API.

 public char charAt(int index) { if ((index < 0) || (index >= count)) { // >= operator is used throw new StringIndexOutOfBoundsException(index); } return value[index + offset]; } 
+4
source

You said that you had already read the documents, so you know that it returns an empty string, because this is what the documents say. But the main reason is as follows:

For a substring, the string indices are interpreted as being between the characters in the string. Thus, index 0 is before the first character, index 1 is between the first and second, and str.length () is after the last character. A substring starting immediately after the last character is "".

This is more important in the version of a substring with two arguments, where the definition of this method saves you from writing special case logic when outputting a substring, which may or may not include the last character.

+4
source

Another way to look at this is that

 str.substring(0, x) + str.substring(x) 

will always be equivalent to s , where 0 <= x <= str.length()

For this, for a break for the only case where x == str.length() will be inconsistent and annoying - for example, you will have to write special cases in analysis loops.

See also the documentation for StringIndexOutOfBoundsException :

Created by the String method to indicate that the index is either negative or larger than the size of the string. For some methods, such as the charAt method, this exception is also thrown when the index is equal to the size of the row.

Please note that the second sentence - charAt should throw an exception when the index is equal to the length of the string, because there is no char at this position for this position. But technically, there is a valid String in this position - this is the zero-length of the String , i.e. "" .

This is consistent with other "slice" operations in java - for example,

 list.subList(list.size(), list.size()) 

returns an empty list, but does not throw an exception.

+3
source

What you offer is illogical. These lines of code

 System.out.println("1 " + "***".substring(0)); System.out.println("2 " + "***".substring(1)); System.out.println("3 " + "***".substring(2)); System.out.println("4 " + "***".substring(3)); 

manufacture

 1 *** 2 ** 3 * 4 

The fourth line of output follows the drawing of another. 3. Throwing an exception makes no sense.

+1
source

Source: https://habr.com/ru/post/1235304/


All Articles