# AP® Computer Science A 4.3 Array Creation and Access

Source: https://lockincs.com/ap-csa/unit-4/4-3-array-creation-and-access/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 4.3.A, 4.3.A.1, 4.3.A.2, 4.3.A.3, 4.3.A.4, 4.3.A.5, 4.3.A.6

An array stores multiple values of the same type, either primitive values or object references, and its length is fixed when it is created. Arrays made with new are filled with default values (0, 0.0, false or null), and an initializer list creates and fills an array in one step. You read and change elements with square brackets and an index from 0 to length - 1; any other index throws an ArrayIndexOutOfBoundsException.

## What you will learn

- 4.3.A Develop code used to represent collections of related data using one-dimensional (1D) array objects.

## 1\. What an array is

An array stores multiple values of the same type 4.3.A.1. Instead of six separate variables for six scores, you use one array variable with six elements. The values can be primitive values (such as `int`, `double` or `boolean`) or object references (such as `String` objects) 4.3.A.1.

The type of an array is written with square brackets after the element type:

```
int[] goals;          // an array of int values
double[] prices;      // an array of double values
String[] players;     // an array of String references
```

Declaring the variable does not create the array yet. An array is an object, so it is created with `new` or with an initializer list (sections 2 and 3). This page is about one-dimensional (1D) arrays: a single row of elements.

**Watch out:** every element must match the declared type.

```
int[] scores = {90, 85.5, 77};
```

Compiler error:

```
error: incompatible types: possible lossy conversion from double to int
```

## 2\. Creating an array with new

When you create an array with the keyword `new`, you give its length, and every element is set to the default value for its type 4.3.A.3:

| Element type | Default value |
| --- | --- |
| `int` | `0` |
| `double` | `0.0` |
| `boolean` | `false` |
| any reference type (`String`, a class) | `null` |

```
int[] goals = new int[4];
double[] prices = new double[3];
boolean[] present = new boolean[2];
String[] players = new String[3];

System.out.println(goals[0] + " " + prices[0] + " " + present[0] + " " + players[0]);
System.out.println("goals has " + goals.length + " elements");
```

Output:

```
0 0.0 false null
goals has 4 elements
```

The length of an array is established when it is created and cannot be changed 4.3.A.2. You read it with the `length` attribute 4.3.A.2. Notice: no parentheses. `length` is an attribute of the array, not a method.

```
int[] goals = new int[4];
int n = goals.length();
```

Compiler error:

```
error: cannot find symbol
```

And because the length cannot change, you cannot assign to it:

```
int[] goals = new int[4];
goals.length = 10;
```

Compiler error:

```
error: cannot assign a value to final variable length
```

## 3\. Initializer lists

An initializer list creates an array and fills it with the values you list, all in one step 4.3.A.4. The length is the number of values in the list.

```
String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri"};
int[] laps = {62, 58, 60};

System.out.println(days.length);
System.out.println(laps.length);
```

Output:

```
5
3
```

Here is how `days` looks in memory. The index row is what you use to reach each element.

| index | 0 | 1 | 2 | 3 | 4 |
| --- | --- | --- | --- | --- | --- |
| `days` | “Mon” | “Tue” | “Wed” | “Thu” | “Fri” |

## 4\. Accessing and modifying elements

Square brackets `[]` with an index access an element, and the same brackets on the left of `=` modify it 4.3.A.5.

```
int[] scores = {7, 4, 9, 6};

System.out.println(scores[2]);     // read index 2

scores[1] = 10;                    // change index 1
scores[3] = scores[3] + 2;         // read index 3, add 2, store it back
scores[0]++;                       // works like any int variable

System.out.println(scores[0] + " " + scores[1] + " " + scores[2] + " " + scores[3]);
```

Output:

```
9
8 10 9 8
```

| index | 0 | 1 | 2 | 3 |
| --- | --- | --- | --- | --- |
| before | 7 | 4 | 9 | 6 |
| after | 8 | 10 | 9 | 8 |

The index can be any `int` expression, such as a variable or `scores.length - 1`:

```
int[] scores = {7, 4, 9, 6};
int i = 1;
System.out.println(scores[i + 1]);
System.out.println(scores[scores.length - 1]);   // always the last element
```

Output:

```
9
6
```

## 5\. Valid indexes and ArrayIndexOutOfBoundsException

The valid index values for an array are 0 through one less than the length of the array, inclusive 4.3.A.6. For an array of length 4, that means 0, 1, 2 and 3. Using any index outside that range results in an `ArrayIndexOutOfBoundsException` 4.3.A.6.

The code compiles, because the compiler does not check index values. The error happens at run time, and the program stops.

```
int[] scores = {7, 4, 9, 6};
System.out.println(scores[3]);
System.out.println(scores[4]);
System.out.println("done");
```

Output:

```
6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
```

Real view: this is the first line the console shows. “done” is never printed, because the exception stops the program. A negative index such as `scores[-1]` causes the same exception.

**Watch out:** `scores[scores.length]` is always out of bounds. The last element is `scores[scores.length - 1]`.

## 6\. Arrays of object references

An array of a reference type stores references, not the objects themselves 4.3.A.1. After `new`, every element is `null`, so you must assign objects before calling methods on them.

```
String[] team = new String[3];
team[0] = "Ana";
team[2] = "Leo";

System.out.println(team[0].length());
System.out.println(team[1]);
System.out.println(team[1].length());
```

Output:

```
3
null
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "team[1]" is null
```

| index | 0 | 1 | 2 |
| --- | --- | --- | --- |
| `team` | “Ana” | null | “Leo” |

Printing a `null` element is fine; it prints `null`. Calling a method on it, such as `team[1].length()`, throws a `NullPointerException`. (The exact wording after the colon depends on your Java version and settings; on the AP® exam you only need the exception name.)

## Common mix-ups

- Not true: The last element of an array is at index length. Indexes start at 0, so the last valid index is length - 1. Using index length throws an ArrayIndexOutOfBoundsException.
- Not true: You get the size of an array with length(), like a String. For an array, length is an attribute with no parentheses: scores.length. For a String it is a method: name.length().
- Not true: A new array of String objects is full of empty strings. Every element of a new array of a reference type is null. There are no String objects in it until you assign them.
- Not true: You can make an array bigger later if you need more room. The length of an array is set when it is created and cannot be changed. You would have to create a new, longer array.

## In 30 seconds

- An array holds many values of one type: primitive values or object references.
- Its length is fixed at creation; read it with the length attribute.
- new fills the array with default values: 0 (int), 0.0 (double), false (boolean), null (reference types).
- An initializer list such as {3, 8, 1} creates and fills an array in one step.
- Square brackets and an index read or change one element; valid indexes are 0 to length - 1.
- Any other index throws an ArrayIndexOutOfBoundsException at run time.

## Checkpoint: 5 practice questions

### Question 1

Consider the following code segment. What is printed as a result of executing the code segment?

```java
double[] a = new double[3];
boolean[] b = new boolean[2];
String[] c = new String[2];
System.out.println(a[1] + " " + b[0] + " " + c[1]);
```

- A. 0 false null
- B. 0.0 true null
- C. 0.0 false null
- D. null null null

**Answer: C.** Arrays created with `new` hold default values: 0.0 for `double`, `false` for `boolean` and `null` for a reference type such as `String`. "0" is the `int` default, not the `double` one; `boolean` elements start `false`, not `true`; and only reference types start as `null`.

### Question 2

Consider the following code segment. What is printed as a result of executing the code segment?

```java
int[] laps = {5, 3, 8, 6};
laps[laps.length - 1] = laps[1] + laps.length;
laps[0]--;
System.out.println(laps[0] + " " + laps[3]);
```

- A. 5 7
- B. 4 12
- C. 4 6
- D. 4 7

**Answer: D.** `laps.length` is 4, so `laps[3]` becomes `laps[1] + 4`, which is 3 + 4 = 7, and `laps[0]--` changes 5 to 4. "5 7" forgets the decrement, "4 12" uses `laps[2]` (8) instead of `laps[1]`, and "4 6" assumes the last element was never changed.

### Question 3

Consider the following array declaration. Which of the following expressions will cause an `ArrayIndexOutOfBoundsException` when it is evaluated?

```java
int[] vals = {2, 4, 6, 8, 10};
```

- A. `vals[vals[2] - 1]`
- B. `vals[vals.length - 5]`
- C. `vals[vals[1]]`
- D. `vals[vals.length / 2]`

**Answer: A.** Valid indexes are 0 to 4. `vals[2]` is 6, so `vals[2] - 1` is 5, which is out of bounds. `vals.length - 5` is 0, `vals[1]` is 4 (the last valid index), and `vals.length / 2` is 2 (integer division), so the other three are valid.

### Question 4

Which of the following statements correctly creates an array that holds the three values 2.5, 4 and 7.0?

- A. `int[] d = {2.5, 4, 7.0};`
- B. `double[] d = {2.5, 4, 7.0};`
- C. `double[] d = new double[3] {2.5, 4, 7.0};`
- D. `double d = {2.5, 4, 7.0};`

**Answer: B.** An initializer list creates and fills an array, and every element must have the array's type; the `int` 4 is stored as 4.0 in a `double` array. An `int` array cannot hold 2.5, Java does not allow a length together with an initializer list, and `double d` is a single variable, not an array, so the other three do not compile.

### Question 5

Consider the following code segment. What is printed as a result of executing the code segment?

```java
String[] names = new String[4];
names[1] = "Kai";
names[3] = names[1] + names.length;
System.out.println(names[3] + " " + names[0]);
```

- A. Kai4 null
- B. Kai4
- C. Kai3 null
- D. Kai null

**Answer: A.** Elements not assigned keep the default `null`, so `names[0]` prints as null. `names.length` is 4 (the length set at creation, not the number of assigned elements), so `names[3]` is "Kai4". "Kai4" alone forgets that `null` is printed, "Kai3" uses the highest index instead of the length, and "Kai" ignores the concatenation.

## Questions students ask

**What is the default value of an array element in Java?**

It depends on the element type: 0 for int, 0.0 for double, false for boolean and null for any reference type, such as String.

**How do I find the length of an array in Java?**

Use the length attribute with no parentheses, for example scores.length. The last valid index is scores.length - 1.

**What causes an ArrayIndexOutOfBoundsException?**

Using an index less than 0 or greater than or equal to the array's length. It compiles, then stops the program at run time when that line runs.

**Can an array hold different types of values?**

No. Every element must have the type given when the array was declared, such as int or String.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
