W3docs

Java Arrays

Declare, initialize, and access elements in Java arrays — fixed-size, indexed sequences of values of the same type.

An array in Java is a fixed-size container that holds a sequence of values of the same type. Once you create it, its length never changes, and every slot can store exactly one value — an int, a String, a reference to any object, anything that fits the declared type.

Arrays are the foundation that the rest of Java's collections (ArrayList, HashMap, and friends) are built on top of. They're not as ergonomic as those higher-level classes, but they're fast, predictable, and you'll see them everywhere — method arguments, return values, performance-sensitive code.

Declaring an array

The type of an array is the element type followed by []:

int[] scores;       // an array of ints
String[] names;     // an array of String references
double[] prices;    // an array of doubles

The square brackets can also go after the variable name — int scores[]; — but the int[] scores form is far more common and is the one you should use.

Declaring a variable doesn't yet create an array; it just gives Java a slot that can hold a reference to one. The slot starts out as null.

Creating an array with new

To actually allocate the array, use new with a length:

int[] scores = new int[5];

This reserves space for 5 ints. Java initializes every slot to the type's default value:

  • numeric types → 0 (or 0.0)
  • booleanfalse
  • char'\u0000' (the null character, code point 0 — not a space)
  • reference types → null

So scores immediately contains {0, 0, 0, 0, 0}. There is no "uninitialized" state — once you've created the array, every position has a defined value.

Array literals

When you know the contents up front, use a literal:

int[] scores = {90, 85, 73, 100, 62};
String[] colors = {"red", "green", "blue"};

The length is inferred from the number of elements. This form is only legal on the same line as the declaration — you can't write scores = {90, 85}; later. For that, use the explicit form:

scores = new int[]{90, 85, 73, 100, 62};

Accessing elements

You read and write elements by zero-based index, using square brackets:

int[] scores = {90, 85, 73, 100, 62};
System.out.println(scores[0]);   // 90
System.out.println(scores[4]);   // 62
scores[2] = 80;                  // replace 73 with 80

Valid indexes run from 0 up to length - 1. An out-of-range index throws ArrayIndexOutOfBoundsException at runtime — Java does not silently return a default or wrap around.

The length property

Every array has a public, read-only length field that reports its size:

int[] scores = {90, 85, 73, 100, 62};
System.out.println(scores.length);   // 5

Note that it's a field, not a method — no parentheses. This is one of Java's small quirks: strings use .length(), arrays use .length. They behave differently from collections (list.size()) too. The shapes are easy to mix up; the compiler will tell you.

length is fixed at creation. To "grow" an array you must allocate a new one and copy — see the Copying arrays chapter.

Arrays hold references, not copies

When the element type is an object, the array stores references, not the objects themselves:

String[] colors = {"red", "green", "blue"};
String first = colors[0];
// first and colors[0] both reference the same "red" string

For primitives this distinction doesn't matter — int values are copied in and out. For objects, mutating an element from outside the array mutates what the array sees, because they share the reference.

Default values and null arrays

A declared but unassigned array variable is null, and reading .length or any index on it throws NullPointerException:

int[] data;            // not initialized
// System.out.println(data.length);  // would throw NullPointerException
data = new int[3];
System.out.println(data.length);     // 3

An empty array — length zero — is a perfectly valid array, and is usually what you want for an "empty" return value, not null:

int[] none = new int[0];
System.out.println(none.length);     // 0

A worked example

java— editable, runs on the server

Printing an array

A common surprise: passing an array straight to System.out.println does not show its contents. It prints the array's type and identity hash instead:

int[] scores = {90, 85, 73};
System.out.println(scores);   // e.g. [I@39ed3c8d  — not the values!

The [I means "array of int", and the hex part is an identity hash, not data. To see the elements, use Arrays.toString from the java.util package:

import java.util.Arrays;

int[] scores = {90, 85, 73};
System.out.println(Arrays.toString(scores));   // [90, 85, 73]

For nested (multi-dimensional) arrays, use Arrays.deepToString instead.

What's next

You can build an array and reach into it by index. The next step is iteration — visiting every element in turn with array loops, so you don't have to spell out scores[0], scores[1], scores[2] one by one.

Practice

Practice
What does new int[3] produce?
What does new int[3] produce?
Was this page helpful?