The main objective of writing this blog is that many of us always confuse that 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 an basic idea about what is an array, basically an array is a collection which store homogenous type 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[0]).

**Why so ? **

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

Let’s have look on bellow image:-

First Element }-x byte |

Second Element }-x byte |

Third Element }-x byte |

Fourth Element } -x byte |

Fifth Element } -x byte |

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

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

If we want to find the next element address of an array then we required to add the no of byte occupied by 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.**

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

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

Nth Element (n) :-

**Address= S+(n-1)*X**

**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[0]) then the location of memory can be find 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 formula also we got the first element 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;

Address=**S+i*X [Here i replace n-1]**

**If i=0 then Address is S+0*X=S**

So here it’s clear that while accessing first element the index become 0 and due to it’s 0 we are getting the starting address for our first element of an array. That’s why array index always starts from 0.

Hope above explanation clear.

Any doubt or any feedback regarding this blog please post it inside comment box.

**#Like#Comment#Feedback**