Java System arraycopy() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.System

Definition

In Java, System.arraycopy(src, srcPos, dest, destPos, length) copies length elements from src, starting at index srcPos, into dest, starting at index destPos. The destination must already exist and be big enough.

It is the fastest standard way to copy part of an array: to grow it, take a slice, or shift elements inside the same array.

A static method of the java.lang.System class, available since the first version of Java (JDK 1.0). Call it on the class name, as in System.arraycopy(...). No import is needed, because classes in java.lang are always visible.

Syntax

Read it as "from src at srcPos, to dest at destPos, this many":

Java
int[] src = {1, 2, 3, 4, 5};
int[] dest = new int[5];
System.arraycopy(src, 0, dest, 0, src.length);  // full copy
System.arraycopy(src, 1, dest, 0, 3);           // dest starts 2, 3, 4
System.arraycopy(src, 0, src, 1, 4);            // shift right inside src
CallWhat it does
System.arraycopy(a, 0, b, 0, a.length)Copies all of a to the start of b.
System.arraycopy(a, i + 1, a, i, n - i - 1)Shifts the tail left by one: removes element i.
System.arraycopy(a, i, a, i + 1, n - i - 1)Shifts the tail right by one: makes room at index i.

The parameters are typed Object, so array types are only checked when the program runs.

Library declarations
Java
// java.lang.System
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

Parameters

ParameterDescription
srcThe source array.
srcPosFirst index to copy in src.
destThe destination array; it is never created or resized.
destPosIndex in dest for the first copied element.
lengthNumber of elements to copy (0 copies nothing).

Return Value

Return ValueDescription
voidNothing. Only the target range of dest is overwritten.

Compare with Similar Methods

Other ways to copy array data:

MethodHow it differs
Arrays.copyOf(a, n)Creates a new array of length n and copies into it.
Arrays.copyOfRange(a, from, to)Returns a new array holding a slice.
for loopWorks, but a forward loop breaks on overlapping shifts (see Example 2).

Java Examples

Example 1 — Grow an Array and Copy a Middle Slice

Arrays have a fixed length. To "add" a score, copy the old values into a bigger array, then copy a slice of two scores into a small array.

Java
import java.util.Arrays;

public class SystemArraycopyGrow {
    public static void main(String[] args) {
        int[] scores = {90, 75, 60, 85};
        int[] bigger = new int[6];
        System.arraycopy(scores, 0, bigger, 0, scores.length);
        bigger[4] = 70;
        System.out.println("Original: " + Arrays.toString(scores));
        System.out.println("Bigger:   " + Arrays.toString(bigger));

        int[] middle = new int[2];
        System.arraycopy(scores, 1, middle, 0, 2);
        System.out.println("Middle:   " + Arrays.toString(middle));
    }
}

How It Works

  1. Room for more. new int[6] starts as six zeros; copying the four scores to index 0 fills the first four places.
  2. One new value. bigger[4] = 70 uses the first free place; index 5 keeps its default 0.
  3. A slice by position. arraycopy(scores, 1, middle, 0, 2) starts at index 1 of scores and copies two elements, 75 and 60. The original array is unchanged.

Example 2 — Remove and Insert by Shifting Elements in the Same Array

Source and destination can be the same array. arraycopy handles the overlap correctly; a naive forward loop does not.

Java
import java.util.Arrays;

public class SystemArraycopyShift {
    public static void main(String[] args) {
        int[] queue = {10, 20, 30, 40, 50};

        System.arraycopy(queue, 2, queue, 1, 3);
        queue[4] = 0;
        System.out.println("Removed 20:  " + Arrays.toString(queue));

        int[] naive = queue.clone();
        for (int i = 1; i < 4; i++) {
            naive[i + 1] = naive[i];
        }
        System.out.println("Naive loop:  " + Arrays.toString(naive));

        System.arraycopy(queue, 1, queue, 2, 3);
        queue[1] = 99;
        System.out.println("Inserted 99: " + Arrays.toString(queue));
    }
}

How It Works

  1. Close the gap. Copying the three elements from index 2 (30, 40, 50) to index 1 overwrites 20; the last slot still holds a stale 50, so it is reset to 0.
  2. Why the loop fails. Copying forward, the loop writes 30 into index 2, then copies that same 30 again and again across the tail.
  3. As if through a temporary array. An overlapping copy behaves as if the source range were first copied to a temporary array, so shifting 30, 40, 50 right keeps all three and frees index 1 for 99.

Example 3 — Too-Long Ranges, Wrong Types, and a Partial Copy

arraycopy checks ranges and types when it runs. Each problem is caught and its exception class name printed.

Java
import java.util.Arrays;

public class SystemArraycopyErrors {
    public static void main(String[] args) {
        int[] small = {1, 2, 3, 4};
        int[] target = new int[10];
        try {
            System.arraycopy(small, 2, target, 0, 5);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Too long: " + e.getClass().getSimpleName());
        }

        long[] longs = new long[4];
        try {
            System.arraycopy(small, 0, longs, 0, 4);
        } catch (ArrayStoreException e) {
            System.out.println("int[] to long[]: " + e.getClass().getSimpleName());
        }

        Object[] mixed = {1, 2, "three", 4};
        Integer[] numbers = new Integer[4];
        try {
            System.arraycopy(mixed, 0, numbers, 0, 4);
        } catch (ArrayStoreException e) {
            System.out.println("String into Integer[]: " + e.getClass().getSimpleName());
        }
        System.out.println("Copied before the error: " + Arrays.toString(numbers));
    }
}

How It Works

  1. Range past the end. Index 2 of a 4-element array leaves only 2 elements, not 5. Nothing is copied; an ArrayIndexOutOfBoundsException (a subclass of IndexOutOfBoundsException) is thrown.
  2. No widening between primitive arrays. An int fits into a long variable, but copying between two different primitive array types throws ArrayStoreException before anything is copied.
  3. Checked element by element. 1 and 2 are Integers and land in numbers; "three" is a String, so the copy stops there and the last two slots stay null.

Frequently Asked Questions

It copies a number of elements from a source index in one array to a destination index in another, existing array.
arraycopy copies into an array you already have. Arrays.copyOf creates and returns a new array of the length you ask for.
Yes. Overlapping ranges are copied as if through a temporary array, so shifting elements left or right works correctly.
No. For object arrays it copies the references, so both arrays point to the same objects.
IndexOutOfBoundsException (in practice ArrayIndexOutOfBoundsException) for bad ranges, ArrayStoreException for incompatible types, and NullPointerException for a null array.

Next: clearProperty()

Remove a system property and get back the value it had.

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

7 people found this page helpful