Why Array indices are 0 based?
“If you’re not confused, you’re not paying attention”
-Tom Peters
Sometime we just learn things and do not question why and how the thing which we are doing behaving in the background or how that things are implemented.
We all learn arrays when we learn to write programs. We have been told that,
“An array starts at 0th index”
We all accepted that the way it is. Some of us questioned that “why it is that way?” Some of us just let that go and moved on.
Now let me give you a situation ,
What if some junior comes to you and asks “Hey XYZ! I was wondering about a thing in array. Do you know why array is zero index based ? Why does not it start from 1 ?”
We cannot simply say , “It is that way only! or I don’t know!”
We all get confused, get embarrassed for some or the other question of someone.Well not anymore, at least not for this question.
Let’s first talk or remind ourselves something about Arrays . Yes we all know,
“An array is a collection of similar data type”
and also this,
Okay! Now let’s talk business!!
Here is a simple explanation of “Why array indices are zero-based”.
When an object is created by using “new” keyword, a memory space is allocated in the heap and a reference is returned. This is also true for arrays, since arrays are objects in Java.
Second thing is array is a contiguous block in memory , that means
an array will occupy continues block or memory blocks in the heap. Just like the following example where we have an array,
Now, lets assume that in memory this array will start from memory address 5000.
“Now why is the next address is 5004 ?”
We know in java “int” takes up 4 byte so,
5000 + 4 (bytes) = 5004 (Still confused ? Here is the great detailing on this.)
So next element address will be 5004 and so on.
“How does this happen?”
Well, to calculate memory address of array element we have some formula,
Lets say array starts at memory address X , size of the element in the array is Y we can calculate memory address of the “i”th element by using following expression = X+i*Y
Address of array[0] = 5000 + 0 (0th element) * 4 (byte) = 5000
Address of array[1] = 5000 + (1 * 4) = 5004
Address of array[2] = 5000 + (2 * 4) = 5008
Address of array[3] = 5000 + (3 * 4) = 5012
Address of array[4] = 5000 + (4 * 4) = 5016
This is what happens when array is zero-index based.
Now lets assume that array actually starts from 5000 but instead of zero-index it is one-index based array ( starts from 1 instead of 0) so what will happen?
Address of array[1] = 5000 + 1(1st element) * 4 (byte) = 5004
It gives us starting memory address 5004 , which is incorrect memory address because our array actually starts from 5000. So every time we have to subtract 1 from the index .
“So in order to get the accurate/correct memory address in heap the position/index of an array needs to start from 0(zero).”
Simple! Isn’t it!
I hope now you would be able to explain why array actually starts from 0 and not from 1.
Thanks for reading . Keep sharing. Keep learning.😊
All suggestions and corrections are welcome!
You are writing about some really amazing , not so known stuff! Keep up the good work...