The reason why it does not throw an exception is because in the two parameter version:
substring(int beginIndex, int endIndex)
endIndex
is exclusive. When the single parameter version you're using specifies the length of the string, the behavior is consistent with the two parameter version, so its treated as exclusive. The result is an empty string.
The actual implementation of the single parameter version is:
public String substring(int beginIndex) {
return substring(beginIndex, count);
}
For reference, the actual implementation of the two parameter version is:
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
return ((beginIndex == 0) && (endIndex == count))
? this
: new String(offset + beginIndex, endIndex - beginIndex, value);
}