Java Reference
In-Depth Information
Table 7.1 Zero-Equivalent
Auto-Initialization Values
Type
Value
int
0
double
0.0
char
'\0'
boolean
false
objects
null
to the array ( temperature ) with a specific index ( [0] , [1] , or [2] ). So, there is an
element known as temperature[0] , an element known as temperature[1] , and an
element known as temperature[2] .
In the temperature array diagram, each of the array elements has the value 0.0 .
This is a guaranteed outcome when an array is constructed. Each element is initialized
to a default value, a process known as auto-initialization.
Auto-Initialization
The initialization of variables to a default value, such as on an array's ele-
ments when it is constructed.
When Java performs auto-initialization, it always initializes to the zero-equivalent
for the type. Table 7.1 indicates the zero-equivalent values for various types. The special
value null will be explained later in this chapter.
Notice that the zero-equivalent for type double is 0.0 , which is why the array ele-
ments were initialized to that value. Using the indexes, you can store the specific
temperature values that are relevant to this problem:
temperature[0] = 74.3;
temperature[1] = 68.4;
temperature[2] = 70.3;
This code modifies the array to have the following values:
[0]
[1]
[2]
temperature
74.3
3
68.4
3
70.3
3
Obviously an array isn't particularly helpful when you have just three values to
store, but you can request a much larger array. For example, you could request an
array of 100 temperatures by writing the following line of code:
double[] temperature = new double[100];
 
 
Search WWH ::




Custom Search