Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
The declaration statement int[] scores tells Java that scores is a variable meant to hold a reference to an array of int values. It does not create an array object, and it does not create four element slots. After this statement, scores exists as a local reference variable, but it does not refer to any array yet.
int[] scores;Because no array object exists, there is no index 0 to read. The expression scores[0] needs scores to refer to an actual array with an element at that index. A local reference variable must also be given a value before Java lets you use it, so trying to read scores[0] at this point is rejected by the compiler.
Immediately after int[] scores;, what exists, and can scores[0] be read?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The expression new int[4] creates one array object with exactly four int slots. Assigning that reference to scores makes the variable point to the new object. Java fills each int slot with its default value, 0, before your code uses the array.
int[] scores = new int[4];
// scores: [0, 0, 0, 0]The length is fixed at the moment of creation. You can assign a value to each existing slot directly by using its index. These assignments change the contents of the same array object, so the four slots become 72, 85, 91, and 68.
scores[0] = 72;
scores[1] = 85;
scores[2] = 91;
scores[3] = 68;When you already know the values, Java lets you create the array and fill its slots with an initializer. The expression {72, 85, 91, 68} creates an array whose length is 4, then stores those values in index order. This is a shorter way to reach the same contents as new int[4] followed by four assignments.
int[] scores = {72, 85, 91, 68};The variable scores still does not contain four integers inside it. It contains a reference to one separate array object. That object contains the four elements, and each element has an index that can be used to read or change its value.
Write the single assignment that changes the value at index 2 from 91 to 95.
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Java numbers the four slots from 0 through 3. You can read scores[0] to get 72, read scores[2] to get 91, or read scores[3] to get 68. You can also assign a new value to one of those positions, such as scores[2] = 95;, which replaces 91 in the third slot.
int first = scores[0]; // 72
int middle = scores[2]; // 91
int last = scores[3]; // 68
scores[2] = 95; // the third slot now contains 95
int length = scores.length; // 4scores.length reports how many slots the array has, so its value is 4. The valid indices are exactly 0 through scores.length - 1, which means 0 through 3 here. scores[4] does not create a fifth slot or return a missing value. It attempts to access outside the array and causes ArrayIndexOutOfBoundsException while the program runs.