Java Arrays.copyOfRange() Method

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

Definition

In Java, Arrays.copyOfRange(original, from, to) returns a new array containing the elements of original from index from up to, but not including, index to. Its length is to - from.

It is Java's way to take a slice or sub-array. Unusually, to may be larger than the array length: the missing positions are padded with 0, false, or null, just like copyOf.

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

Syntax

Pass the array, a start index, and an end index (exclusive).

Java
int[] a = {10, 20, 30, 40, 50};
int[] mid = Arrays.copyOfRange(a, 1, 4);   // [20, 30, 40]
int[] tail = Arrays.copyOfRange(a, 3, a.length); // [40, 50]
int[] pad = Arrays.copyOfRange(a, 3, 7);   // [40, 50, 0, 0]
CallWhat it does
Arrays.copyOfRange(a, i, j)Elements at indexes i to j - 1.
Arrays.copyOfRange(a, k, a.length)Everything from index k to the end.
Arrays.copyOfRange(a, 0, n)The first n elements, the same as copyOf(a, n).
Arrays.copyOfRange(a, i, i)An empty array.

Overloads exist for all eight primitive types, for T[], and for U[] with a target array type (Class<? extends T[]> newType).

Library declarations
Java
// java.util.Arrays (since Java 6)
public static int[] copyOfRange(int[] original, int from, int to)
public static char[] copyOfRange(char[] original, int from, int to)
public static <T> T[] copyOfRange(T[] original, int from, int to)
public static <T, U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)
// also long[], short[], byte[], float[], double[], boolean[]

Parameters

ParameterDescription
originalThe source array. Must not be null.
from (int)First index to copy, inclusive. Must be between 0 and original.length.
to (int)End index, exclusive. Must be at least from; may be greater than original.length.

Return Value

Return ValueDescription
New arrayA new array of length to - from holding the copied slice, padded with default values if to goes past the end.

Compare with Similar Methods

Related copying tools:

MethodHow it differs
Arrays.copyOf(a, n)Always starts at index 0.
System.arraycopy(src, i, dest, j, n)Copies into an existing array at any position; no padding, stricter bounds.
Arrays.stream(a, from, to)Processes a range without copying it into a new array.
list.subList(from, to)The List version: a view of a range, not a copy.

Java Examples

Example 1 — Cut Pieces Out of a char Array

Take the first four letters, the last two, and a short slice from the middle.

Java
import java.util.Arrays;

public class ArraysCopyOfRangeDemo {
    public static void main(String[] args) {
        char[] word = {'j', 'a', 'v', 'a', 'f', 'x'};
        char[] first4 = Arrays.copyOfRange(word, 0, 4);
        char[] last2 = Arrays.copyOfRange(word, 4, word.length);

        System.out.println(new String(first4));
        System.out.println(new String(last2));
        System.out.println(Arrays.toString(Arrays.copyOfRange(word, 1, 3)));
    }
}

How It Works

  1. End index is excluded. copyOfRange(word, 0, 4) copies indexes 0, 1, 2, and 3, which spell java.
  2. Up to the end. Using word.length (6) as the end index copies indexes 4 and 5, f and x.
  3. Length is to minus from. The middle slice (1, 3) has 3 - 1 = 2 elements: a and v.
  4. Characters to text. new String(...) joins each copied char[] into a word for printing.

Example 2 — An End Index Beyond the Array

Unlike most index-based methods, to may run past the last element.

Java
import java.util.Arrays;

public class ArraysCopyOfRangePadding {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};
        System.out.println(Arrays.toString(Arrays.copyOfRange(a, 2, 6)));
        System.out.println(Arrays.toString(Arrays.copyOfRange(a, 4, 4)));
        System.out.println(Arrays.toString(Arrays.copyOfRange(a, 4, 7)));
    }
}

How It Works

  1. Partly real, partly padding. Range 2 to 6 has length 4: indexes 2 and 3 exist (3 and 4) and the other two slots become 0.
  2. Equal indexes. When from equals to, the result has length 0 and prints as [].
  3. Starting exactly at the end is legal. from = 4 equals a.length, which is still allowed, so the result is three padding zeros.
  4. Where padding stops. Only from has to be inside 0 to length; one step further triggers an exception, as the next example shows.

Example 3 — Split an Array into Chunks, Then Break the Rules

Process a task list two at a time, then trigger both exceptions.

Java
import java.util.Arrays;

public class ArraysCopyOfRangeChunks {
    public static void main(String[] args) {
        String[] tasks = {"t1", "t2", "t3", "t4", "t5"};
        for (int start = 0; start < tasks.length; start += 2) {
            int end = Math.min(start + 2, tasks.length);
            System.out.println(Arrays.toString(Arrays.copyOfRange(tasks, start, end)));
        }

        try {
            Arrays.copyOfRange(tasks, 3, 1);
        } catch (IllegalArgumentException e) {
            System.out.println("from > to: IllegalArgumentException");
        }
        try {
            Arrays.copyOfRange(tasks, 6, 8);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("from > length: ArrayIndexOutOfBoundsException");
        }
    }
}

How It Works

  1. Step by the chunk size. start takes the values 0, 2, and 4, and each chunk covers two indexes.
  2. A short last chunk. Math.min(start + 2, tasks.length) caps the final end index at 5, so the last chunk holds only t5 instead of being padded with null.
  3. Backwards range. (3, 1) would mean a length of -2, so the method throws IllegalArgumentException.
  4. Start past the end. from = 6 is greater than the length 5, which the documentation answers with ArrayIndexOutOfBoundsException.

Frequently Asked Questions

No. from is inclusive and to is exclusive, so the result has to - from elements.
Yes. The extra positions are filled with 0, false, or null. Only from must stay between 0 and the array length.
IllegalArgumentException when from > to, ArrayIndexOutOfBoundsException when from < 0 or from > length, and NullPointerException when the array is null.
Use Arrays.copyOfRange(array, from, to). It returns a new array; changing it does not change the original.
copyOf always copies from index 0 and takes a length. copyOfRange takes a start and an end index.

Next: Arrays.deepEquals()

Compare nested arrays such as int[][] by their contents.

deepEquals() 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.

12 people found this page helpful