Arrays in Java
An array is a fixed-size, ordered collection of elements all of the same type, stored contiguously in memory and accessed by a zero-based index. int[] numbers = new int[5]; creates space for exactly five ints, each initialized to 0 by default, and that size can never change afterward — growing or shrinking an array actually means creating an entirely new one and copying elements across.
Arrays can also be created with values already filled in using literal syntax, int[] numbers = {1, 2, 3};, and every array knows its own length via the .length field — not a method call, unlike String's .length() — which is a small but frequently-tripped-over inconsistency.
Fixed size
An array's length is set at creation and can never change afterward.
Zero-based indexing
The first element is at index 0, and the last is at index length - 1.
Default values
A newly created array's elements start at a type-appropriate default: 0 for numeric types, false for boolean, null for object references.
Array literal syntax
int[] numbers = {1, 2, 3}; creates and fills an array in one step, sized exactly to match the values given.
The length field
Arrays expose their size through .length, a field access, not a method call like String's .length().
public class ArrayDemo { public static void main(String[] args) { int[] scores = {85, 92, 78}; scores[1] = 95; System.out.println("First: " + scores[0]); System.out.println("Length: " + scores.length); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.