Learn Java
import com.learnjava.datastructurescollections.Arrays;

Arrays in Java

@Concept

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.

@HowItWorks
1

Fixed size

An array's length is set at creation and can never change afterward.

2

Zero-based indexing

The first element is at index 0, and the last is at index length - 1.

3

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.

4

Array literal syntax

int[] numbers = {1, 2, 3}; creates and fills an array in one step, sized exactly to match the values given.

5

The length field

Arrays expose their size through .length, a field access, not a method call like String's .length().

@Example
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);
}
}
Output
First: 85
Length: 3
1scores is created and filled with three values in one step using array literal syntax.
2scores[1] = 95; overwrites the second element, at index 1, in place — from 92 to 95.
3scores[0] is read directly, still holding its original value of 85.
4scores.length reads the field storing the array's fixed size, which is 3 regardless of what values are inside it.
@UseCases
Storing a fixed number of related values, like the twelve months or a hand of cards
Passing a whole block of data to a method as a single argument
Building the backing storage other data structures like ArrayList use internally
Debugging an ArrayIndexOutOfBoundsException caused by an index outside 0 through length minus 1

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.