Why don't array starts with 1 ? Why java creators designed to starts with 0 rather than 1
Index of Array
You must be signed in to leave a comment
19 February, 11:13
Not just Java creators did. When you declare an array you get a reference of a type. With the type (eg. int) you know each element needs 4 bytes in memory, the reference tells you the start address of the array in the computers main memory. To now get the second element (index 1) you just do a address + 1 x 4 bytes the 3rd element is address + 3 x 4 bytes the first element address + 0 x 4 bytes The same formula for each element If you start the index at 1 then the compiler would need to extra calculations to get the address of the element (or you use a different formula for the first element and that's even more cpu cycles), eg. for the first elenent (index 0) address + (1 - 1) * 4 bytes You probably know that looping over arrays is a task you do pretty often. Sometimes over thousands or even millions of elements. Then this sums up. Instead of extra calculations you (or better the language creators) could have decided to use more memory and save the address of each element in an index.