Quick revision
The short answer
System.arraycopy copies a range into an array we already have, Arrays.copyOf creates a new array of a chosen length (truncating or padding with default values), Arrays.copyOfRange creates a new array from a slice, and clone() creates a new array of the same type and length. All four are shallow: for an array of objects, the copy holds the same references, so both arrays point to the same element objects, and for a 2D array only the outer array is copied while the rows stay shared.
What to remember
In this answer, we compared the four ways to copy arrays. System.arraycopy writes a range into an existing array and handles overlapping ranges in the same array correctly; Arrays.copyOf creates a new array of a new length, truncating or padding with defaults; Arrays.copyOfRange copies a slice into a new array; and clone() creates a same-sized copy of the same type without a cast since Java 5. All of them are shallow, so reference elements are shared and a cloned 2D array still shares its rows, which is why a deep copy needs an explicit loop.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
System.arraycopy copies a range into an array we already have, Arrays.copyOf creates a new array of a chosen length (truncating or padding with default values), Arrays.copyOfRange creates a new array from a slice, and clone() creates a new array of the same type and length. All four are shallow: for an array of objects, the copy holds the same references, so both arrays point to the same element objects, and for a 2D array only the outer array is copied while the rows stay shared.
Why this question is asked
Copying arrays looks too simple to ask about, yet it is behind a surprising number of bugs: a cache that changes when a caller edits a returned array, a matrix whose copy changes together with the original, or a resize that quietly loses data. Interviewers ask this to check that we know which method fits which job and, above all, what shallow means. In this answer, we will recap the difference between shallow and deep copies, go through each of the four methods with its exact behaviour, see the 2D array trap, and then write a deep copy when we really need one.
1A Quick Recap: Shallow and Deep Copies
An array variable holds a reference to an array object. For an int[], the array object contains the int values themselves. For a String[] or a User[], the array object contains references to other objects that live elsewhere on the heap.
A shallow copy creates a new array object and copies each slot as it is. For primitives that means copying the values, so the two arrays are completely independent. For references, it copies the references, so both arrays point to the same element objects. A deep copy would also copy each element object, so that changing an element through one array cannot affect the other.
Every built-in way to copy an array in Java is shallow. That is not a weakness, it is simply all the JDK can do in general, because it does not know how to copy an arbitrary element object.
2System.arraycopy: Copying Into an Existing Array
System.arraycopy(src, srcPos, dest, destPos, length) does not create anything. It copies length elements from src starting at srcPos into dest starting at destPos. The destination must already exist and be large enough.
Notice the second example. Source and destination are the same array and the ranges overlap. A naive forward loop would read values it has already overwritten and produce [1, 1, 1, 1, 1]. System.arraycopy is specified to behave as if the source range were first copied to a temporary array, so the result is correct. That is why ArrayList uses it to shift elements when we insert or remove in the middle.
It is also a JIT intrinsic, which means HotSpot replaces the call with highly optimised machine code. When something goes wrong, it throws: IndexOutOfBoundsException for a bad range, ArrayStoreException if the element types do not fit (for example copying a String[] into an Integer[]), and NullPointerException for a null array.
3Arrays.copyOf and Arrays.copyOfRange: New Arrays
Arrays.copyOf(original, newLength) creates a new array of newLength and copies as much of the original as fits. If the new length is shorter, the rest is cut off. If it is longer, the extra slots get default values: 0 for numbers, false for booleans, null for references. Arrays.copyOfRange(original, from, to) creates a new array holding the slice from index from (inclusive) to to (exclusive).
These are the methods to use when we want a new array, such as growing a buffer or taking a slice. Internally they allocate the new array and call System.arraycopy. The overload that takes a Class lets us choose the new array's type, which is how ArrayList.toArray(T[]) produces a correctly typed array.
4Clone(): A Same-Sized Copy of the Same Type
Every array has a public clone() method that returns a new array of the same type and length with the same contents. Since Java 5, the return type of clone() on an array is the array's own type, so int[] copy = original.clone() needs no cast. Before Java 5, clone() returned Object and we had to cast.
The second half shows shallow copying in action. The clone has its own slots, so names2[0] = new StringBuilder() would not affect names. But both slots referred to the same StringBuilder, so changing that object is visible through both arrays. With immutable elements such as String or Integer, this never matters, which is why shallow copies are usually enough.
5The 2D Array Trap
A 2D array is an array of row arrays. Cloning it copies only the outer array, the one holding the row references. The rows themselves are shared.
| Method | Creates a new array? | Length | Copy depth |
|---|---|---|---|
System.arraycopy | No, writes into dest | any range we give | shallow |
Arrays.copyOf | Yes | new length, pad or truncate | shallow |
Arrays.copyOfRange | Yes | to - from | shallow |
array.clone() | Yes | same as original | shallow (outer array only for 2D) |
So for a 2D primitive array, a deep copy means cloning the outer array and every row, which the loop does (Arrays.stream(grid).map(int[]::clone).toArray(int[][]::new) is the same thing in one line). For arrays of mutable objects, a deep copy means creating a copy of each element, using a copy constructor or a copy method of that class.
6Follow-up questions
Which of these is fastest?
For the same amount of data they are all very close, because copyOf, copyOfRange and clone allocate a new array and then use the same fast copy as System.arraycopy. The choice should be about intent: arraycopy when the target exists, copyOf to resize, copyOfRange for a slice, clone for a same-sized copy.
Is Arrays.copyOf safe on a 2D array?
It creates a new outer array, but the rows are still the same objects, exactly like clone(). It is useful for growing the number of rows, not for getting independent rows.
How do we deep copy an array of our own objects?
We copy each element ourselves, for example Arrays.stream(users).map(User::new).toArray(User[]::new) with a copy constructor. Serialisation tricks also produce deep copies but are slow and fragile, so we avoid them. Often the better answer is to make the element class immutable, so a shallow copy is all we ever need.
7Conclusion
In this answer, we compared the four ways to copy arrays. System.arraycopy writes a range into an existing array and handles overlapping ranges in the same array correctly; Arrays.copyOf creates a new array of a new length, truncating or padding with defaults; Arrays.copyOfRange copies a slice into a new array; and clone() creates a same-sized copy of the same type without a cast since Java 5. All of them are shallow, so reference elements are shared and a cloned 2D array still shares its rows, which is why a deep copy needs an explicit loop.
Open with
All four are shallow copies, and that is the first thing I would say. System.arraycopy copies a range into an array that already exists; it does not allocate, it is a JIT intrinsic, and it is specified to handle overlapping ranges in the same array correctly, which is why ArrayList uses it to shift elements.
Then, if they want more
Arrays.copyOf creates a new array with whatever length I ask for, truncating if it is shorter or padding with zeros, false or nulls if it is longer, and there is an overload that changes the array type. Arrays.copyOfRange creates a new array from a slice, from inclusive to exclusive.
And clone() on an array returns a new array of the same type and length; since Java 5 it returns the array type, so no cast is needed. Shallow means that for an array of objects, the new array holds the same references, so if the elements are mutable, changing one through the copy shows up in the original.
With a 2D array that is a classic bug: clone copies only the outer array, so the rows are shared, and a deep copy needs a loop that clones each row, or copies each element object for mutable elements.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare System.arraycopy, Arrays.copyOf, Arrays.copyOfRange and clone() for copying...": https://scrivio-interview-library.netlify.app/java/arrays/7523ea872a/