# Reason That Array Index Starts From Zero (0)

The main objective of writing this blog is that many of us always get confused about why the array index starts from Zero(0); so in this blog I am going to tell you the main reason behind this.

I assume that you have a basic idea about what is an array. Basically an array is a collection which stores homogenous types of data.

In the array if we want to access any element then we can use the array index to access an element. So when we are accessing the first element of an array then we can use index as 0 (arr).

Why is this so?

To understand this we need to understand how the JIT compiler looks up an array index inside memory.

Let’s have a look at the below figures,

• First Element }-x byte
• Second Element }-x byte
• Third Element }-x byte
• Fourth Element } -x byte
• Fifth Element } -x byte

In the above figures we saw that each element occupies ‘x’ byte in the memory and the starting address of the memory location is ‘S’.

So from the above we clearly see that the first element address will be ‘S’.

If we want to find the next element address of an array then we are required to add the number of bytes occupied by the first element(X) to the starting address(S).

Hence in the above figure each element consumes X byte and the starting address is S so the next address will be ‘S’+ X.

Likewise the third element will be at ‘S’+2*X and so on.

Hence if we find out nth element address then the formula derived from the above is :-

Nth Element (n)

Where “n-1”indicate the index of an array.

When I say I want to access the first element inside an array (i.e arr) then the location of memory can be found out by;

S+(n-1)*X

Where n=1 (First element we are accessing).

Then the first element memory location is S+(1-1)*X

=>S+0*X=S

By using this formula also we get  thatthe first element's address will be S.

As I said above n-1 is our index which means the first index of an array always starts from 0.

The above formula can also be written as;

If we want to access i+1 th element where i is the index of an array then;