初學者常問:`int[] arr = new int[10];` 為什麼 `int` 寫兩次?
int[] arr // 左側:宣告變數 arr 的型別為「int 陣列」
= // 指派
new int[10] // 右側:在堆疊配置一個「int 陣列」物件,長度 10
**不能**。左側宣告型別與右側建立型別必須相容(相同或為父類別/介面)。例如:
Number[] nums = new Integer[10]; // 合法:Integer 是 Number 子類別
int[] arr = new long[10]; // 編譯錯誤:long[] 不是 int[]
若同時宣告並指定初始值,可省略右側 `new int[]`:
int[] arr = {1, 2, 3, 4, 5}; // 編譯器自動推斷型別與長度
總結:`int[] arr` 是**變數契約**,`new int[10]` 是**物件實例**,兩者分工明確,型別必須一致。