Why is array indexing done with 0 and not with 1 in programming languages like Java? I am totally new to Java, any explanation is welcomed.
-
4I think java language designers just want to keep it close to C++, so they could migrate to Java easily. – Leo Jul 19 '14 at 14:02
-
7Please see [Why are zero-based arrays the norm?](http://programmers.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm) where this question has already been answered. – DavidPostill Jul 19 '14 at 14:03
-
Also http://developeronline.blogspot.com.br/2008/04/why-array-index-should-start-from-0.html – Leo Jul 19 '14 at 14:07
-
1Array indexes are the **spaces between the elements**--not the elements. The "0th" index is the space before the *first* element. In a three element array, index 2 (length - 1) refers to the third--the last--element. – aliteralmind Jul 19 '14 at 15:20
6 Answers
Java uses zero-based indexing because c uses zero-based indexing. C uses zero-based indexing because an array index is nothing more than a memory offset, so the first element of an array is at the memory it's already pointing to, *(array+0)
.
See also Wikipedia's array indexing in different languages?

- 7,611
- 5
- 39
- 71

- 53,822
- 15
- 101
- 132
To expand upon @Kevin's answer, I take this quote from an answer on Programmers.SE:
The index in an array is not really an index. It is simply an offset that is the distance from the start of the array. The first element is at the start of the array so there is no distance. Therefore the offset is 0.
Further, if you want to learn more about how different languages do their array indexing, then look at this exhaustive list on Wikipedia.

- 1
- 1

- 3,288
- 6
- 29
- 51
A quote by Dijkstra, from Why numbering should start at zero (1982):
When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the next vexing question is what subscript value to assign to its starting element. Adhering to convention a) yields, when starting with subscript 1, the subscript range 1 ≤ i < N+1; starting with 0, however, gives the nicer range 0 ≤ i < N. So let us let our ordinals start at zero: an element's ordinal (subscript) equals the number of elements preceding it in the sequence. And the moral of the story is that we had better regard —after all those centuries!— zero as a most natural number.
A discussion about this article can be found in Lambda the Ultimate - Why numbering should start at 0.

- 28,903
- 6
- 107
- 121
-
3Interesting quote. With subscript starting at 1, the subscript range is equivalently `1 <= i <= N`, so Dijkstra's argument is not particularly convincing. – jII Jul 19 '14 at 14:12
-
1@jesterII: 1 ≤ i < N+1, but you can make your range effectively anything – Engineer2021 Jul 19 '14 at 14:13
-
2This is just an opinion. The only real answer is "because the language was designed this way". – Stefano Sanfilippo Jul 19 '14 at 14:13
-
@StefanoSanfilippo I agree, for many languages it's probably mostly for historical reasons. Still, language designers probably had more reasoning than just copying it from older languages. Dijkstras opinion shows why it would make sense to start at 0. – kapex Jul 19 '14 at 14:20
-
And @jesterII opinion shows that starting at 0 does not make more sense than starting at 1. It's just opionions. I guess they went for the 0-based convention because C/C++ arrays are 0-based and they did not want to force C/C++ programmers into learning yet another convention. – Stefano Sanfilippo Jul 19 '14 at 14:36
-
-
But you could certainly ask James Gosling. He is well alive! – Stefano Sanfilippo Jul 19 '14 at 14:38
-
-
3@staticx He probably would have given the same answer as Knuth: http://xkcd.com/163/ – kapex Jul 19 '14 at 15:12
-
@Kapep thats the really important quote from Knuth. imho a quote about starting at 0 or 1 shouldnt be taken all too serious and I am certain that both Knuth and Dijkstra have/had more interesting things to say ;) – 463035818_is_not_an_ai Jun 08 '17 at 11:42
I Have presented answer to this question in the diagram below which i have written in a piece of paper which is self explanatory
Main Steps:
- Creating Reference
- Instantiation of Array
- Allocation of Data to array
----------
- Also note when array is just instantiated .... Zero is allocated to all the blocks by default until we assign value for it
- Array starts with zero because first address will be pointing to the reference (i:e - X102+0 in image)
Note: Blocks shown in the image is memory representation
I Have also posted this answer here

- 42,072
- 54
- 195
- 297
To summarize his argument:
When working with sub-sequences of natural numbers, the difference between the upper bound and the lower bound should be the length of the sub-sequence. The indices of an array can be thought of as a special kind of such a sub-sequence. The lower bound should be inclusive, the upper bound should be exclusive. In other words, the lower bound should be the first index of the array. Otherwise, we risk having to have a lower bound in the unnatural numbers for some sub-sequences. If we want to maintain conditions (1) and (2), then we effectively have two choices for upper and lower bounds: 1 <= i < N+1 or 0 <= i < N. Clearly, putting N+1 in the range is ugly, so we should prefer indexing starting from 0.
It's all legacy from the times when programming languages like C were merely high level assemblers. Programming mavericks spent their wonderful lives doing pointer arithmetic so it became their second nature to count from zero. Now, they are passing this legacy to many modern languages. You can even read statements as "Zero is the most natural number.". Zero is not a natural number. People don't count from zero in real life, mathematicians don't, physicists don't, statisticians don't count from zero... it's only the computer science.
Further, you don't say "I have zero apples" to express the fact that you don't have any apples, otherwise following same logic, you would say "I don't have minus one apples" to express the fact that you have one apple :P

- 167
- 2
- 8