The question "Why" can be considered philosophical or academic, and provoke answers along the lines of "This is as it is."
However, from a more general, abstract point of view, this is the right question when considering alternatives: you can imagine two forms of this method:
String substringByIndices(int startIndex, int endIndex);
and
String substringByLength(int startIndex, int length);
, : .
, , . :
int startIndex = ...;
int endIndex = ...;
String s = string.substringByLength(startIndex, endIndex-startIndex);
int startIndex = ...;
int length = ...;
String s = string.substringByIndices(startIndex, startIndex+length);
, , , +1 -1 , .
, : , +1 -1:
int startIndex = 12;
int length = 34;
String s = string.substringByIndices(startIndex, startIndex+length);
System.out.println(s.length() == length);
- , for -loops,
for (int i=startIndex; i<endIndex; i++) { ... }
, - . , , .
, , , , :
API.
, List subList (int, int):
List<E> subList(int fromIndex, int toIndex)
fromIndex, inclusive toIndex, .
. API-, , , .