int[] arr = new int[4]; // 內存圖如下
理解: 凡是new關鍵字創建的對象,jvm都會在堆內存中開闢新的空間創建一個新的對象。
所以對於上述的=相當於把數組對象內存地址賦值給arr變量。
int[] arr1 = new int[2];
int[] arr2 = arr1; // 公用一個內存地址,所以下面的操作arr1和arr2指向同一個堆內存地址
arr1[1]=10;
arr2[1]=20;
arr1[1] = 20;
// 二維數組
int[][] arr = new int[3][4];
class Student{
String name;
// 使用static修飾country,那麼這時候country就是一個共享的數據
static String country = "China";
// 在這個位置使用了static的區別, 這個數據就不會存在對象堆內存中維護了,而是存放到了數據共存去來維護。這是最關鍵的區別... 在內存中的表現如下圖..
// String country = "China"; 這種情況的country成員變量是存放在堆內存的位置中,所以有沒有static是完全不同的兩種格式
// 構造函數
public Student(String name) {
this.name = name;
this.country = country;
}
}
class Demo9 {
public static void main(String[] args) {
Student s1 = new Student("GOD");
Student s2 = new Student("PIG");
}
}