Java Reference
In-Depth Information
An array is a sequence of values of the same type.
The
new
operator merely constructs the array. You will want to store a reference to
the array in a variable so that you can access it later.
The type of an array variable is the element type, followed by
[]
. In this example, the
type is
double[]
, because the element type is double. Here is the declaration of an
array variable:
double[] data = new double[10];
That is, data is a reference to an array of floating-point numbers. It is initialized with
an array of 10 numbers (see
Figure 1
).
You can also form arrays of objects, for example
BankAccount[] accounts = new BankAccount[10];
288
289
Figure 1
An Array Reference and an Array
When an array is first created, all values are initialized with 0 (for an array of
numbers such as
int[]
or
double[]
),
false
(for a
boolean[]
array), or
null
(for an array of object references).
Each element in the array is specified by an integer index that is placed inside square
brackets (
[]
). For example, the expression