Java Reference
In-Depth Information
where intExp is any expression that evaluates to a positive integer. Also, the value of
intExp specifies the number of elements in the array.
You can combine the statements in Lines 1 and 2 into one statement as follows:
dataType[] arrayName = new dataType[intExp];
//Line 3
We typically use statements similar to the one in Line 3 to create arrays to manipulate
data.
When an array is instantiated, Java automatically initializes its elements to their default
values. For example, the elements of numeric arrays are initialized to 0 , the elements
of char arrays are initialized to the null character, which is '\u0000' , the elements of
boolean arrays are initialized to false .
EXAMPLE 9-1
The statement:
int [] num = new int [5];
declares and creates the array num consisting of 5 elements. Each element is of type int .
The elements are accessed as num[0] , num[1] , num[2] , num[3] , and num[4] . Figure 9-1
illustrates the array num .
num
num[0]
num[1]
num[2]
num[3]
num[4]
0
0
0
0
0
FIGURE 9-1 Array num
 
Search WWH ::




Custom Search