int[] a; // It does not yet initialize a with an actual array.int[] a = new int[100];var a = new int[100];int[] smallPrimes = { 2, 3, 5, 7, 11, 13 };
array of numbers, initialized with zero
array of boolean, initialized with false
array of objects, initialized with special value null
Arrays in Java vs. so in C++
int[] a = new int[100]; // Java// notint a[100]; // C++// butint* a = new int[100]; // C++
In Java, the [] operator is predefined to perform bounds checking. Furthermore, there is no pointer arithmetic—you can’t increment a to point to the next element in the array.
you can assign to an array, but …
smallPrimes = new int[] { 17, 19, 23, 29, 31, 37 }; // new produce an 无名array// is shorthand forint[] anonymous = { 17, 19, 23, 29, 31, 37 };smallPrimes = anonymous;
You can copy one array variable into another, but then both variables refer to the same array
if you really want to copy an array, use method: copyOf: