Java Arrays.fill() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Arrays

Definition

In Java, Arrays.fill(a, val) stores val in every element of the array a. Arrays.fill(a, from, to, val) does the same for indexes from up to, but not including, to.

New arrays start with default values (0, false, or null). fill is the quick way to start with something else, such as -1 for "not set yet", or to reset an array you are reusing. It changes the array in place and returns nothing.

A static method of java.util.Arrays (Java 1.2+): call it on the class, as in Arrays.fill(...); there is no Arrays object to create.

Syntax

Fill everything, or give a range first.

Java
int[] a = new int[5];
Arrays.fill(a, 7);          // [7, 7, 7, 7, 7]
Arrays.fill(a, 1, 3, 0);    // [7, 0, 0, 7, 7]
String[] s = new String[3];
Arrays.fill(s, "?");        // [?, ?, ?]
CallWhat it does
Arrays.fill(a, val)Every element becomes val.
Arrays.fill(a, from, to, val)Only indexes from to to - 1 change.
Arrays.fill(chars, ' ')Blank out a char[] buffer.
for (int[] row : grid) Arrays.fill(row, v)Fill a 2D array one row at a time.

Both forms exist for long[], int[], short[], char[], byte[], boolean[], float[], double[], and Object[]: eighteen overloads in total.

Library declarations
Java
// java.util.Arrays (since Java 1.2)
public static void fill(int[] a, int val)
public static void fill(int[] a, int fromIndex, int toIndex, int val)
public static void fill(boolean[] a, boolean val)
public static void fill(Object[] a, Object val)
public static void fill(Object[] a, int fromIndex, int toIndex, Object val)
// same pair for long[], short[], char[], byte[], float[], double[], boolean[]

Parameters

ParameterDescription
aThe array to fill. Must not be null.
fromIndex, toIndex (optional)Range to fill: fromIndex inclusive, toIndex exclusive.
valThe value stored in each element of the range.

Return Value

Return ValueDescription
voidNothing. The array passed in is modified.

Compare with Similar Methods

Other ways to set many elements:

MethodHow it differs
Arrays.setAll(a, i -> ...)Computes a different value for each index.
Arrays.copyOf(a, n)Creates a new array; padding uses defaults, which fill can then overwrite.
new int[n]Already filled with 0, so filling with 0 right after creation is unnecessary.
Arrays.parallelSetAll(a, ...)Like setAll, using several threads for large arrays.

Java Examples

Example 1 — Start an Array with -1 and Draw a Line

Mark every seat as "not assigned" and build a divider line from a char[].

Java
import java.util.Arrays;

public class ArraysFillDemo {
    public static void main(String[] args) {
        int[] seats = new int[5];
        Arrays.fill(seats, -1);
        System.out.println(Arrays.toString(seats));

        char[] line = new char[12];
        Arrays.fill(line, '=');
        System.out.println(new String(line));
    }
}

How It Works

  1. A sentinel value. A new int[5] holds zeros; filling it with -1 makes "no one assigned yet" easy to tell apart from seat number 0.
  2. In place. fill returns void, so the result is printed from seats itself.
  3. Characters too. The fill(char[], char) overload writes twelve '=' characters into line.
  4. Array to text. new String(line) turns the twelve characters into one string for printing.

Example 2 — Fill Only Part of an Array

Book three slots in a calendar and mark the weekend in a list of seven days.

Java
import java.util.Arrays;

public class ArraysFillRange {
    public static void main(String[] args) {
        boolean[] booked = new boolean[8];
        Arrays.fill(booked, 2, 5, true);
        System.out.println(Arrays.toString(booked));

        String[] week = new String[7];
        Arrays.fill(week, "work");
        Arrays.fill(week, 5, 7, "rest");
        System.out.println(Arrays.toString(week));
    }
}

How It Works

  1. End index excluded. fill(booked, 2, 5, true) sets indexes 2, 3, and 4; index 5 stays false.
  2. Untouched slots keep their value. The rest of booked keeps the boolean default, false.
  3. Layered fills. The whole week array is first set to "work", then the range 5 to 7 is overwritten with "rest".
  4. Up to the length. toIndex 7 equals week.length, which is the largest value allowed.

Example 3 — Filling a 2D Array: Shared Rows vs Separate Rows

Passing one row object to fill puts the same row in every slot.

Java
import java.util.Arrays;

public class ArraysFillSharedRow {
    public static void main(String[] args) {
        int[][] wrong = new int[3][];
        Arrays.fill(wrong, new int[2]);
        wrong[0][0] = 9;
        System.out.println("shared rows: " + Arrays.deepToString(wrong));

        int[][] right = new int[3][2];
        for (int[] row : right) {
            Arrays.fill(row, 7);
        }
        right[0][0] = 9;
        System.out.println("own rows:    " + Arrays.deepToString(right));
    }
}

How It Works

  1. One row object. new int[2] runs once, and fill(Object[], Object) stores that same reference in all three slots of wrong.
  2. One change, three rows. Setting wrong[0][0] = 9 changes the shared row, so every row appears to start with 9.
  3. Separate rows. new int[3][2] creates three independent rows, and the loop fills each one with 7.
  4. Only the first row changes. In right, the 9 appears in row 0 only, which is usually what you want.

Frequently Asked Questions

Create the array, then call Arrays.fill(array, value). For example, int[] a = new int[10]; Arrays.fill(a, -1);
Yes. Arrays.fill(a, from, to, value) fills indexes from to to - 1.
Loop over the rows and call Arrays.fill(row, value) on each. Calling Arrays.fill(grid, new int[n]) puts the same row object in every slot.
No. It returns void and changes the array you pass in.
NullPointerException for a null array, IllegalArgumentException or ArrayIndexOutOfBoundsException for a bad range, and ArrayStoreException when an Object[] cannot hold the value.

Next: Arrays.hashCode()

Compute a hash code from the contents of an array.

hashCode() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful