Instantiate an array object using
new, the data type, and an array size in square brackets
int nums; nums = new int;
The second line constructs a new array object with 10 integer elements, all initialized to 0, and stores the reference into
int moreNums; int size = 7; moreNums = new int[size];
You can declare and instantiate all at once:
String names = new String;
The elements of the array,
String references, are initialized to
As objects, arrays also have a useful property:
names.lengthwould be 3.
You can reassign a new array to an existing variable:
int nums; nums = new int; nums = new int;
The original ten-element array is no longer referenced by
nums, since it now points to the new, larger array.