Java數組與棧內存、堆內存


package ch4;

/**
 * Created by Jiqing on 2016/11/9.
 */
public class ArrayInRam {
    public static void main(String[] args) {
        int[] a = {5,7,20};
        int[] b = new int[4];
        System.out.println("b數組的長度為:"+b.length);
        for(int i = 0,len = a.length;i<len;i++) {
            System.out.println(a[i]);
        }

        // 循環輸出b數組的元素
        for (int i = 0,len = b.length;i<len;i++) {
            System.out.println(b[i]);
        }

        // b引用指向a引用的數組
        b = a;
        System.out.println("b數組的長度為:"+b.length);
        for (int i = 0,len = b.length;i<len;i++) {
            System.out.println(b[i]);
        }

        a[1] = 11;
        System.out.println(b[1]); // 值變了

    }
}

package ch4;

/**
 * Created by Jiqing on 2016/11/9.
 */
public class Person {
    public int age; // 年齡
    public double height; // 身高
    public void info() {
        System.out.println("我的年齡是:"+age+",我的身高是:"+height);
    }
}

package ch4;

/**
 * Created by Jiqing on 2016/11/9.
 */
public class Student {
    public static void main(String[] args) {
        Person[] students;
        students = new Person[2];
        Person zhang = new Person();
        zhang.age = 15;
        zhang.height = 158;

        Person lee = new Person();
        lee.age = 16;
        lee.height = 161;

        students[0] = zhang;
        students[1] = lee;
        lee.info(); // 我的年齡是:16,我的身高是:161.0
        students[1].info(); // 我的年齡是:16,我的身高是:161.0
    }
}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM