Java 引用類型數組

引用類型變量可使用類、接口或數組來聲明。數組

數組引用變量是存放在棧內存(stack)中,數組元素是存放在堆內存(heap)中,經過棧內存中的指針指向對應元素在堆內存中的位置來實現訪問。spa

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
    }
}
相關文章
相關標籤/搜索