skip to content

System.arraycopy and Array Copying

System.arraycopy is an intrinsic native block copy that Arrays.copyOf builds on, and it beats a hand-written loop. Interviewers use it when discussing how ArrayList grows.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What is System.arraycopy and what do its five parameters mean?

level: juniorimportance: must knowfreq 55%

answer

  1. src, srcPos, dest, destPos, length
  2. copies a range, not the whole array
  3. src and dest may be the same array
  4. native method — the JDK's copy primitive
  5. NPE / IndexOOB / ArrayStoreException

basics

~10 s

System.arraycopy copies a range of elements from one array to another. You pass the source array, where to start reading, the destination array, where to start writing, and how many elements to copy.

solid answer

~40 s

System.arraycopy(src, srcPos, dest, destPos, length) is a native method that copies length elements from src starting at index srcPos into dest starting at index destPos. The source and destination can be the same array (it handles overlap correctly). It is the fundamental low-level copy primitive in the JDK: ArrayList, StringBuilder, Arrays.copyOf, and many collections call it under the hood. Because it is implemented in native/intrinsic code, the JIT can turn it into a fast block memory move rather than an element-by-element loop. It throws NullPointerException if either array is null, ArrayStoreException on incompatible element types, and IndexOutOfBoundsException if any index or the length is out of range.

go deeper

for a junior

Can state the parameter order and explain it copies a range of elements into an existing destination array.

for a middle

Knows the exceptions it throws, that source and destination may be the same array, and that it is void (writes in place).

for a senior

Explains that it is the JDK's underlying copy primitive used by ArrayList/StringBuilder/Arrays, and the difference between primitive (checked-first) and reference (per-element checked) copies.

for a principal

Discusses it as a JIT intrinsic, when partial copies can occur on ArrayStoreException, and API design tradeoffs of an Object-typed native primitive vs a typed wrapper.

## What an array is An **array** in Java is a fixed-size, contiguous block of memory holding elements of one type, accessed by integer **index** (position) starting at 0. Arrays cannot grow; to "resize" you allocate a new array and copy the old elements over. That copying is exactly what `System.arraycopy` exists to make fast. ## The method `System` is a built-in JDK class of utility methods. `arraycopy` is a **native** method on it — meaning its body is written in the JVM's own code (C/C++/intrinsic), not in Java. Its full signature is: ``` public static native void arraycopy( Object src, int srcPos, Object dest, int destPos, int length) ``` The five parameters, in order: - **`src`** — the array to copy *from* (the source). - **`srcPos`** — the index in `src` where reading begins. - **`dest`** — the array to copy *into* (the destination). It can be the **same** array as `src`. - **`destPos`** — the index in `dest` where writing begins. - **`length`** — how many elements to copy. So it copies `src[srcPos .. srcPos+length-1]` into `dest[destPos .. destPos+length-1]`. ## A concrete example Given `src = [10, 20, 30, 40, 50]` and a `dest` of size 5 all zeros, calling `System.arraycopy(src, 1, dest, 0, 3)` reads elements at source indices 1,2,3 (`20,30,40`) and writes them to destination indices 0,1,2, giving `dest = [20, 30, 40, 0, 0]`. ## Errors it can throw - **`NullPointerException`** — if `src` or `dest` is `null`. - **`IndexOutOfBoundsException`** (specifically `ArrayIndexOutOfBoundsException`) — if any of `srcPos`, `destPos`, or `length` is negative, or if `srcPos+length` exceeds `src.length`, or `destPos+length` exceeds `dest.length`. - **`ArrayStoreException`** — if an element in the source range cannot be stored into the destination because of incompatible types (e.g. copying an `Object[]` that contains a `String` into an `Integer[]`). Importantly, the bounds and type checks happen **before** any copying for primitive arrays, so a failed call doesn't leave the destination half-copied in those cases; for reference arrays where element types are checked per-element, a partial copy can occur before an `ArrayStoreException`. ## Why it matters It is the lowest-level copy primitive in the JDK. `ArrayList.add` (when growing), `StringBuilder.append`, `Arrays.copyOf`, `Arrays.copyOfRange`, and `Collections` all delegate to it. Knowing its parameter order and that source/dest may be the same array is everyday Java fluency.

  • Does System.arraycopy create the destination array for you?
    No. It writes into an array you have already allocated; if dest is null you get a NullPointerException, and if dest is too small you get an IndexOutOfBoundsException. Use Arrays.copyOf when you want a freshly-sized array created for you.
  • What happens if length is 0?
    Nothing is copied and no exception is thrown (as long as the arrays are non-null and the positions are valid for a zero-length range). It is a legal no-op.

saying these in an interview costs you the question

  • Thinking it returns a new array — it returns void and writes into an existing dest
  • Forgetting the parameter order (mixing up srcPos and destPos)
  • Believing it allocates the destination — you must allocate dest yourself
  • Assuming it copies the entire source array regardless of length

context

open as a page

How does Arrays.copyOf relate to System.arraycopy, and when would you use each?

level: middleimportance: should knowfreq 48%

basics

~20 s

Arrays.copyOf allocates a brand-new array of the size you ask for and uses System.arraycopy internally to fill it. Use copyOf when you want a fresh array; use arraycopy when you already have the destination and want to copy into a specific spot.

open as a page

Why is System.arraycopy typically faster than copying an array with a manual for-loop?

level: seniorimportance: should knowfreq 42%

basics

~20 s

System.arraycopy is implemented inside the JVM as a single block memory copy, so it moves many bytes at once instead of one element per loop iteration. A hand-written for-loop does an array bounds check and a separate store on every element.

open as a page

How do you use System.arraycopy to insert or remove an element within the same array, and why is the in-place overlap correct?

level: seniorimportance: should knowfreq 35%

basics

~20 s

To remove an element you shift the elements after it one slot left; to make room for an insert you shift them one slot right. You call System.arraycopy with the same array as both source and destination — it handles the overlapping ranges correctly.

open as a page

What exceptions can System.arraycopy throw, and when can a copy be left partially completed?

level: middleimportance: nice to knowfreq 28%

basics

~20 s

It throws NullPointerException if either array is null, IndexOutOfBoundsException if any index or the length goes out of range, and ArrayStoreException if an element type does not fit the destination. For object arrays, an ArrayStoreException can happen after some elements were already copied.

open as a page