Page 333 - AP Computer Science A, 7th edition
P. 333

Initialization
In Java, an array is an object; therefore, the keyword new must be used in its creation. The size of an array remains fixed once it has been created. As with String objects, however, an array reference may be reassigned to a new array of a different size.
Ex a m pl e
All of the following are equivalent. Each creates an array of 25 double values and assigns the reference data to this array.
1. double[] data = new double[25];
2. double data[] = new double[25];
3. double[] data; data = new double[25];
A subsequent statement like
data = new double[40];
reassigns data to a new array of length 40. The memory allocated for the previous data array is recycled by Java’s automatic garbage collection system.
When arrays are declared, the elements are automatically initialized to zero for the primitive numeric data types (int and double), to false for boolean variables, or to null for object references.
It is possible to declare several arrays in a single statement. For example,
int[] intList1, //declares intList1 and intList2; intList2 to
//contain int values int[] arr1 = new int[15], arr2 = new





















































































   331   332   333   334   335