Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

Java System.arraycopy() method

Reference for System.arraycopy(): copy a range of array elements, overlapping ranges, exceptions, shallow copies of objects and when to prefer Arrays.copyOf.

System.arraycopy() copies a range of elements from one array into another (or into a different part of the same array). It is a native method and is usually the fastest way to move bulk data, which is why ArrayList, StringBuilder and Arrays.copyOf use it internally.

Both arrays must already exist, and the destination must be big enough. The method never allocates anything.

Java
System.arraycopy(source, srcPos, dest, destPos, length);

Try it

Java Copying ranges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Exceptions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Name Description
src Source array.
srcPos Index of the first element to copy.
dest Destination array.
destPos Index in the destination where the first element lands.
length Number of elements to copy.

Returns: nothing (void).

Throws:

  • NullPointerException if src or dest is null.
  • ArrayStoreException if an argument is not an array, the component types are incompatible (for example int[] into long[]), or an element cannot be stored in the destination. With reference arrays, elements before the failing one have already been copied.
  • IndexOutOfBoundsException if srcPos, destPos or length is negative or the range exceeds either array.

Usage notes

  • Overlap is safe. When src and dest are the same array, the copy behaves as if the source range was first copied into a temporary array. That is what makes shifting elements for insert and remove work.
  • Shallow copy. For object arrays only references are copied; both arrays then point to the same objects.
  • Primitive types must match exactly. int[] to long[] is an error even though int widens to long. Reference arrays follow assignment rules (String[] into Object[] is fine).
  • length == 0 is legal, as long as the positions are within 0..array.length inclusive.
  • Easier alternatives. Arrays.copyOf(array, newLength) and Arrays.copyOfRange(array, from, to) allocate the destination for you, and array.clone() duplicates a whole array. arraycopy is for filling an existing array.
  • Performance. A memory move without a per-element loop; the JIT replaces it with intrinsic code. Copying a million int values takes about a millisecond.
  • Not thread safe. Another thread modifying either array while it is copied can produce a mix of old and new values.
  • Multi-dimensional arrays: copying int[][] copies row references, so the rows are shared. Copy each row to get a deep copy.