Java 引用类型数组

引用类型变量可以使用类、接口或数组来声明。

数组引用变量是存放在栈内存(stack)中,数组元素是存放在堆内存(heap)中,通过栈内存中的指针指向对应元素在堆内存中的位置来实现访问。

public class Student {
    public String name;
    public int age;
    public char sex;
}
public class Array {

    public static void main(String[] args) {
        
        //基本数据类型的值是以数值存在的
        //基本数据类型的数组
        int[] a = {1,2,3};
        System.out.println(a[0]);
        
        //引用数据类型的值是以对象存在的
        //引用类型的数组
        //          s            stu1            stu2
        Student[] s = {new Student(), new Student()};
        System.out.println(s[0]);
        System.out.println(s[1]);
        
        //        栈            堆                堆
        //        a -------->    a[0]
        //                    a[1]
        //                    a[2]

        //        s -------->    s[0] --------> stu1
        //                    s[1] --------> stu2
    }
}

 

posted @ 2017-11-28 08:46  LeeAaron  阅读(7008)  评论(0编辑  收藏  举报