Are upper bounds of indexed ranges always assumed to be exclusive?

Posted by polygenelubricants on Stack Overflow See other posts from Stack Overflow or by polygenelubricants
Published on 2010-03-13T22:26:51Z Indexed on 2010/03/13 22:35 UTC
Read the original article Hit count: 204

So in Java, whenever an indexed range is given, the upper bound is almost always exclusive.

From java.lang.String:

substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1

From java.util.Arrays:

copyOfRange(T[] original, int from, int to)

from - the initial index of the range to be copied, inclusive
to - the final index of the range to be copied, exclusive.

From java.util.BitSet:

set(int fromIndex, int toIndex)

fromIndex - index of the first bit to be set.
toIndex - index after the last bit to be set.

As you can see, it does look like Java tries to make it a consistent convention that upper bounds are exclusive.

My questions are:

  • Is this the official authoritative recommendation?
  • Are there notable violations that we should be wary of?
  • Is there a name for this system? (ala "0-based" vs "1-based")

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about indexing