Arrays
In short: A fixed, typed collection of elements under one shared name — the size is fixed at creation and can’t be changed afterwards.
In more detail: Declaration e.g. int[] numbers = new int[5]; or directly with values int[] numbers = {1, 2, 3};. Access happens via a zero-based index. If a variable size is needed, Java uses ArrayList from the Collections framework instead.
In Depth
Arrays in Java are objects (even arrays of primitive types like int[]) and carry their length as a public field .length (no parentheses, unlike String.length() or list.size() — a common source of confusion for beginners). Accessing an invalid index throws an ArrayIndexOutOfBoundsException at runtime, not at compile time:
int[] numbers = {10, 20, 30};
System.out.println(numbers.length); // 3
System.out.println(numbers[0]); // 10
System.out.println(numbers[3]); // ArrayIndexOutOfBoundsException! Valid: 0..2
int[] empty = new int[5]; // all values automatically initialized to 0
String[] words = new String[3]; // all values automatically initialized to nullBecause the size is fixed, an array can’t “grow” or “shrink” — at most you can create a new, larger array and copy the old values over with Arrays.copyOf(), which is exactly what ArrayList does internally. In return, arrays are somewhat more performant and memory-efficient than ArrayList, because they can store primitive values directly (int[] instead of List<Integer>, which would have to “box” every value individually as an object). Rule of thumb: fixed, known size and performance matters → array; size changes at runtime → ArrayList.
The java.util.Arrays class
For common array operations, Java ships with the helper class Arrays, instead of having to write them yourself with a loop:
int[] numbers = {5, 3, 8, 1};
Arrays.sort(numbers); // sorts in place: [1, 3, 5, 8]
int pos = Arrays.binarySearch(numbers, 8); // 3 - only valid on sorted arrays
String text = Arrays.toString(numbers); // "[1, 3, 5, 8]" - for debugging/println
int[] copy = Arrays.copyOf(numbers, 6); // new array, length 6, rest padded with 0
boolean equal = Arrays.equals(numbers, copy); // false - compares content, not referenceImportant: System.out.println(numbers) alone does NOT print readable content, but something like [I@1b6d3586 (type + hash code) — arrays don’t override toString(), which is why Arrays.toString() must be used explicitly.
Arrays as objects and reference types
An array name in Java is a reference, not a value type — if an array is passed to a method or assigned to another variable, both share the same underlying array in memory:
int[] original = {1, 2, 3};
int[] reference = original;
reference[0] = 99;
System.out.println(original[0]); // 99 - the same array instance, no copy!
int[] realCopy = original.clone(); // creates an independent copyMultidimensional and jagged arrays
Java also allows “jagged arrays” (arrays of arrays with different lengths per row), since a multidimensional array is internally nothing other than an array whose elements are themselves arrays — see Multidimensional Arrays for details.
See also: Index, Multidimensional Arrays, ArrayList, For Loop