Java Arrays

1、Arrays of Primitives-基础类型数组

 

    When you create an array of primitive elements, the array holds the actual values for those elements. For instance, Figure 2−2 shows what an array of six integers (1452, 1472, 1483, 1495, 1503, 1519) referred to from the variable life would look like with regards to stack and heap memory.

 int[] life={1452, 1472, 1483, 1495, 1503, 1519};

 

Java Arrays

Figure 1 :The stack and heap memory for an array of primitives.

 

2、Arrays of Objects-对象数组

     Unlike an array of primitives, when you create an array of objects, they are not stored in the actual array. The array only stores references to the actual objects, and initially each reference is null unless explicitly initialized. (More on initialization shortly.) Figure 2 shows what an array of Object elements would look like where the elements are as follows:

· Leonardo da Vinci's country of birth, Italy

· An image of his painting The Baptism of Christ

· His theories (drawings) about helicopters and parachutes

· An image of the Mona Lisa

· His country of death, France

    The key thing to notice in Figure 2 is that the objects are not in the array: only references to the objects are in the array.

 

Java Arrays

Figure 2 :The stack and heap memory for an array of primitives.

你可能感兴趣的:(java)