JAVATM- The Beginnings

Arrays

Suppose you wanted to store the values of three integers. You could declare three separate int variables and initialize them to the three values:

        

        int x = 1;

        int y = 2;

        int z = 3;

This works well for a few numbers, but what if you needed to store the values of 1000 integers?

You would have to write 1000 declarations, each containing a unique identifier for the variable name. Fortunately, Java provides arrays.

Section 1 - Arrays Defined

An array is a construct that stores several values of the same type in contiguous memory. An array named A which stores the int values 1, 2, and 3 looks as follows:

                                   

The three int values in array A occupy contiguous memory locations starting at address 0x1000.

The first int resides at memory location 0x1000. Since an int require four bytes of memory, the second int resides at 0x1000 + 4 = 0x1004. The third int resides at 0x1000 + 8 = 0x1008.

Each value stored in an array is called an element.

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.



Back to ralphlecessi.com