Why is array indexing in Java start with 0?

前端 未结 6 847
深忆病人
深忆病人 2020-12-01 15:06

Why is array indexing done with 0 and not with 1 in programming languages like Java ? I am totally new to java any explanation is welcomed.

6条回答
  •  孤城傲影
    2020-12-01 15:44

    I Have presented answer to this question in the diagram below which i have written in a piece of paper which is self explanatory

    Main Steps:

    1. Creating Reference
    2. Instantiation of Array
    3. Allocation of Data to array

    ----------

    • Also note when array is just instantiated .... Zero is allocated to all the blocks by default until we assign value for it
    • Array starts with zero because first address will be pointing to the reference (i:e - X102+0 in image)

    Note: Blocks shown in the image is memory representation

    I Have also posted this answer here

提交回复
热议问题