Arrays
An array is a fixed-length sequence of slots of one type. You pick the length when you create it. Indexes start at 0. length is a field, not a method.
Detailed Explanation
When you need many values of the same type and you know how many, an array is the simplest store. int[] scores = new int[3] gives three ints, all 0. String[] names = new String[3] gives three references, all null. The length never changes. To grow, you allocate a new array and copy.
Arrays are objects. The variable holds a reference. Passing an array into a method does not copy the slots; the method can write xs[0] = 9 and the caller sees it. clone() and Arrays.copyOf make a new array with the same (or copied) slot values. For object arrays that is a shallow copy: the objects themselves are shared.
The type of the array is part of the value. String[] is not Object[] in the way you might hope. Java array types are covariant: a String[] may be assigned to an Object[] variable. Storing an Integer into that Object[] then throws ArrayStoreException at runtime. Generics were invented partly to avoid this hole. Prefer List<String> when the size changes or you want compile-time safety without covariance tricks.
Indexes are ints. xs[i] throws ArrayIndexOutOfBoundsException if i < 0 or i >= xs.length. There is no negative-index-from-the-end syntax. Multidimensional arrays are arrays of arrays: int[][] grid = new int[2][3] is two rows of three. Rows can have different lengths if you allocate them yourself.
You reach for an array when the length is known, when you talk to an API that requires an array (main(String[] args), varargs), or when you need a compact block of primitives without boxing. You reach for ArrayList when the length changes. You reach for List.of or array.clone() when you want to hand out a snapshot.
The cost of arrays is the fixed length and the lack of a rich API. Sorting, searching, and filling live on java.util.Arrays. There is no contains that does the right thing for objects unless you write it. Bounds errors are runtime only. Enhanced for hides the index; classic for is required when the index matters.
Code Examples
import java.util.Arrays;
public class Scores {
public static void main(String[] args) {
int[] scores = new int[3]; // length 3, values 0, 0, 0
scores[0] = 10;
scores[1] = 7;
scores[2] = 9;
System.out.println(scores.length); // prints: 3
System.out.println(Arrays.toString(scores)); // prints: [10, 7, 9]
}
}import java.util.Arrays;
public class Copy {
static void bump(int[] xs) {
xs[0] += 1; // same array the caller holds
}
public static void main(String[] args) {
int[] a = {1, 2, 3};
bump(a);
int[] b = Arrays.copyOf(a, a.length); // new array, same numbers
b[0] = 99;
System.out.println(a[0]); // prints: 2
System.out.println(b[0]); // prints: 99
}
}Common Gotchas
`xs[xs.length]` is out of bounds
last index is `length - 1`.
`Object[] slot = strings; slot[0] = 1` throws `ArrayStoreException`
array covariance is checked at runtime.
`new String[3]` holds nulls
enhanced `for` will NPE if you call methods on each slot before filling.
`length` is a field
`xs.length()` does not compile. `String.length()` is a method; do not mix them.