Java arrays
An array is Java's most fundamental way to store a fixed-size sequence of values. Reading and tracing array code—following index access, assignment, and loops by hand—is a skill you will use in almost every Java program and technical interview.
Core idea
A Java array holds a fixed number of values of the same type. Each value sits at a numbered index starting at 0. Once created, the length cannot change, but the values at each index can be reassigned freely.
Declaring and initializing arrays
There are two common ways to create an array.
Array literal — list the values in braces. Java infers the length from the number of values provided.
int[] scores = {88, 95, 72, 100, 61};
New keyword — specify the length. Java fills every slot with the default value (0 for int, false for boolean, null for objects).
int[] scores = new int[5]; // {0, 0, 0, 0, 0}
Indexing and the length property
Each element is accessed by placing its index inside square brackets. The array stores the index-to-value mapping permanently until an assignment changes it.
Index diagram for int[] nums = {10, 30, 50, 70, 90}
Index: 0 1 2 3 4 Value: 10 30 50 70 90 nums[0] → 10 nums[2] → 50 nums[4] → 90 ← last index = length - 1 = 4 nums.length → 5 ← the count, not the last index
Accessing nums[5] on a five-element array throws an ArrayIndexOutOfBoundsException because index 5 does not exist.
Modifying elements
Assign a new value to any index with the = operator. The old value is gone; all future reads of that index return the new value.
int[] arr = {4, 8, 15, 16, 23};
arr[1] = 100;
System.out.println(arr[1]); // 100, not 8
Reassignment does not affect any other index. The array is still five elements long, and indices 0, 2, 3, 4 are unchanged.
Iterating with loops
Two loop patterns cover the vast majority of array iteration in Java.
Indexed for loop
The classic loop gives you access to the index itself, which is useful when you need to skip elements, go backwards, or compare adjacent positions.
int[] arr = {3, 6, 9, 12};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
// prints 3, 6, 9, 12 on separate lines
The loop condition is i < arr.length, not i <= arr.length. Using <= would access one past the end and throw an exception.
Enhanced for-each loop
When you only need the value and not the index, the for-each loop is shorter and harder to miswrite. It always goes from index 0 to the last element.
int[] arr = {3, 6, 9, 12};
for (int x : arr) {
System.out.println(x);
}
// prints 3, 6, 9, 12 on separate lines
Worked examples
Example 1: index access and length
int[] data = {5, 12, 3, 8, 20};
System.out.println(data[2]);
System.out.println(data.length);
Trace:
data[2] → index 2 holds 3 → prints 3 data.length → 5 elements → prints 5
Output (two lines):
3 5
Example 2: modify then read
int[] arr = {10, 20, 30};
arr[0] = 99;
System.out.println(arr[0]);
System.out.println(arr[1]);
Trace:
arr starts as {10, 20, 30}
arr[0] = 99 → arr is now {99, 20, 30}
arr[0] → 99 (the updated value)
arr[1] → 20 (unchanged)
Output:
99 20
Example 3: summing with a for-each loop
int[] nums = {4, 7, 2, 9};
int sum = 0;
for (int x : nums) {
sum += x;
}
System.out.println(sum);
Trace (one loop iteration per row):
Before loop: sum = 0 x = 4 → sum = 0 + 4 = 4 x = 7 → sum = 4 + 7 = 11 x = 2 → sum = 11 + 2 = 13 x = 9 → sum = 13 + 9 = 22 After loop: sum = 22
Output: 22
Example 4: printing every other element
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i += 2) {
System.out.println(arr[i]);
}
Trace:
i = 0 → arr[0] = 1, prints 1 i = 2 → arr[2] = 3, prints 3 i = 4 → arr[4] = 5, prints 5 i = 6 → 6 < 5 is false, loop exits
Output:
1 3 5
Refreshable sample
Try one Java array question
Select "No loops" or "With loops", read the snippet, and type what you think will print to the console. Whitespace between lines does not matter—just make sure each value is on its own line when the output spans multiple lines.
Key things to remember
- Indices are zero-based: the first element is always at index 0.
- The last valid index is
length − 1. Accessingarr[arr.length]throws an exception. - Arrays created with
new int[n]start at 0; arrays created with a literal start at the listed values. - Assignment at an index overwrites the value. Every subsequent read of that index returns the new value.
- For-each loops always visit in order from index 0 upward. Use an indexed loop when you need the index itself.
When you are comfortable, the next natural step is Java 2D arrays (matrices), which add a second index for rows and columns.
Open Java arrays practice