Array Creation
and Access
Master the fundamentals of declaring, initializing, and accessing arrays in Java: the building blocks of organized data.
What Is an Array?
An array is a fixed-size, ordered collection of elements that all share the same data type. Think of it as a row of numbered boxes: each box holds one value, and you locate any box by its index.
Why Use Arrays?
Arrays in Memory
In Java, an array is an object stored on the heap. The variable you declare holds a reference (a pointer) to the actual data. This reference model means arrays are passed by reference to methods.
Declaring an Array
Java offers two syntaxes for array declaration. Both are valid, but the first form is preferred in AP CS A because the brackets clarify the type.
int[] scores;
String[] names;
// Also valid: brackets with the variable
int scores[];
String names[];
Two Ways to Create an Array
// Creates 5 slots,
// all initialized to 0
// Creates + fills in
// one step
Default Values by Type
| Element Type | Default Value | Example |
|---|---|---|
| int, short, byte, long | 0 | new int[3] → {0, 0, 0} |
| double, float | 0.0 | new double[2] → {0.0, 0.0} |
| boolean | false | new boolean[2] → {false, false} |
| String (or any object) | null | new String[2] → {null, null} |
Accessing Elements
Use the array name followed by square brackets containing the index. You can both read and write with this syntax.
// Read: get element at index 2
int val = nums[2]; // val = 30
// Write: set element at index 0
nums[0] = 99; // nums is now {99, 20, 30, 40}
.length vs .length() vs .size()
| Feature | Array | String | ArrayList |
|---|---|---|---|
| Syntax | arr.length | str.length() | list.size() |
| Type | Field (property) | Method call | Method call |
| Parentheses? | No | Yes | Yes |
| Resizable? | No | No | Yes |
Bounds Checking
Java checks every array access at runtime. If you use an index \(< 0\) or \(\ge .length\), the JVM throws an ArrayIndexOutOfBoundsException.
Step-by-Step Approach
Example 1: Creating and Reading
System.out.println(temps[0]); // 72.5
System.out.println(temps[3]); // 69.8
System.out.println(temps.length); // 4
Example 2: Modifying Elements
// colors = {null, null, null}
colors[0] = "Red";
colors[1] = "Blue";
colors[2] = "Green";
// Overwrite index 1
colors[1] = "Yellow";
// colors = {"Red", "Yellow", "Green"}
Example 3: Traversing an Array
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
System.out.println(sum); // 45
The Enhanced For Loop
for (int n : nums) {
System.out.print(n + " ");
}
// Output: 5 12 8 3 17
Common Mistakes
int[] arr; then accessing \(arr[0]\) without new. The reference is null.Question 1 of 5
What is the output of the following code?int[] a = {4, 8, 15, 16, 23};
System.out.println(a[2]);
Question 2 of 5
Which line of code correctly creates an array of 5 doubles, all initialized to 0.0?
Question 3 of 5
What happens when you execute arr[arr.length] on an array of size 5?
Question 4 of 5
What is the default value of each element in new String[3]?
Question 5 of 5
Consider the following code. What is the value of result?int[] vals = {2, 4, 6, 8};
vals[1] = vals[3];
int result = vals[0] + vals[1];