File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes where array index starts from 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "where array index starts from 0" Watch "where array index starts from 0" New topic
Author

where array index starts from 0

Deepika Wadhwa
Ranch Hand

Joined: Aug 05, 2000
Posts: 57
Hi all,
i have a basic confusion..
why does array index start from zero ?
is there any specific reasond this ?

thanx in advance
Deepika
sandeep jakkaraju
Greenhorn

Joined: Sep 05, 2001
Posts: 8
I dont know the answer to this ...
but i can say that ..the concept of first element of an array being 0 (ZERO) is INDIAN !! for the obvious reasons ;-)

Originally posted by Deepika Wadhwa:
Hi all,
i have a basic confusion..
why does array index start from zero ?
is there any specific reasond this ?

thanx in advance
Deepika

Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
There is no reason for it other than maybe it was copied from another language like C/C++ ? In COBOL arrays start at 1.
It sometimes catches me out ;-D
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
You may be right when you say it is copied from C/C++ (which may have copied ASM 68000). In these languages, tab[i] refers to a memory adress situated at tab+i*sizeof(tab elements). So it is easier to refer to 0 as being the first element, otherwise it would have needed one more arithmetic operation: tab+(i-1)*sizeof(tab elements).
Any other ideas?
W.
matt hooker
Ranch Hand

Joined: Jul 26, 2001
Posts: 46
Yeah, Wilfred is spot on. The reasons for array initialisation at 0 are very low level memory address type stuff, and indeed, low level logic dictates starting at 0, so there is an (uneccessary??) overhead in manipulating this default value for the purposes of our code.


Its not what you do, its the way you say you've done it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where array index starts from 0
 
Similar Threads
variable of interest
[Interview Q] Why does array index begin with 0 instead of 1 ???
whats wrong with my code ?
Facing a problem with java.util.logging
Why starting index of array is 0