Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 680249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:21:04+00:00 2026-05-14T01:21:04+00:00

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

  • 0

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”)

CLARIFICATION: I fully understand that a collection of N objects in a 0-based system is indexed 0..N-1. My question is that if a range (2,4) given, it can be either 3 items or 2, depending on the system. What do you call these systems?

AGAIN, the issue is not “first index 0 last index N-1” vs “first index 1 last index N” system; that’s known as the 0-based vs 1-based system.

The issue is “There are 3 elements in (2,4)” vs “There are 2 elements in (2,4)” systems. What do you call these, and is one officially sanctioned over the other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:21:04+00:00Added an answer on May 14, 2026 at 1:21 am

    Credit goes to FredOverflow in his comment saying that this is called the “half-open range”. So presumably, Java Collections can be described as “0-based with half-open ranges“.

    I’ve compiled some discussions about half-open vs closed ranges elsewhere:


    siliconbrain.com – 16 good reasons to use half-open ranges (edited for conciseness):

    • The number of elements in the range [n, m) is just m-n (and not m-n+1).
    • The empty range is [n, n) (and not [n, n-1], which can be a problem if n is an iterator already pointing the first element of a list, or if n == 0).
    • For floats you can write [13, 42) (instead of [13, 41.999999999999]).
    • The +1 and -1 are almost never used, when handling ranges. This is an advantage if they are expensive (as it is for dates).
    • If you write a find in a range, the fact that there was nothing found can easily indicated by returning the end as the found position: if( find( [begin, end) ) == end) nothing found.
    • In languages, which start the array subscripts with 0 (like C, C++, JAVA, NCL) the upper bound is equal to the size.

    Half-open versus closed ranges

    Advantages of half-open ranges:

    • Empty ranges are valid: [0 .. 0]
    • Easy for subranges to go to the end of the original: [x .. $]
    • Easy to split ranges: [0 .. x] and [x .. $]

    Advantages of closed ranges:

    • Symmetry.
    • Arguably easier to read.
    • ['a' ... 'z'] does not require awkward + 1 after 'z'.
    • [0 ... uint.max] is possible.

    That last point is very interesting. It’s really awkward to write an numberIsInRange(int n, int min, int max) predicate with a half-open range if Integer.MAX_VALUE could be legally in a range.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting an error that says java.lang.NullPointerException: null from a code whenever I run
I'm moving from Java to C++ right now and I'm having some difficulties whenever
Whenever I try to post to an https endpoint, I get a javax.net.ssl.SSLException: java.lang.NullPointerException
I have been moving my Java projects from Jdeveloper over to Eclipse whenever I
I have a problem, that whenever i try to create java process from shell
Hi i'm writing a program thats using 2 dimensional int arrays in java. whenever
I have always wondered why the garbage collector in Java activates whenever it feels
Whenever I start my app, I get a java.lang.IllegalArgumentException: column '_id' does not exist
Whenever I see reference to memory in Java, the various spaces are always prefixed
I am getting a java.lang.ArrayStoreException whenever I attempt to run an update query using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.