Time complexity of System.arraycopy...?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
System.arraycopy is one of the fastest ways to copy array data in Java, so developers often wonder whether it has some special time complexity beyond an ordinary loop. The short answer is no: copying n elements is still linear work, but the JVM can execute that linear work much more efficiently than hand-written Java code.
The Complexity Is Linear in the Number of Copied Elements
The signature takes a source array, source start position, destination array, destination start position, and length. The important input for complexity is length, because that is how many elements must be moved.
If source.length is 5, the runtime copies 5 elements. If it is 5 million, the runtime copies 5 million elements. That makes the complexity O(n) where n is the number of copied entries.
This is true for primitive arrays and object reference arrays alike. The operation cannot be O(1) because the runtime still has to read and write each slot that is being copied.
Why It Is Usually Faster Than a Manual Loop
Linear complexity does not mean all O(n) implementations are equal. System.arraycopy is a JVM intrinsic on common runtimes, which means the JVM can replace the method call with highly optimized native code or platform-specific instructions.
Compare these two versions:
Both versions are linear, but the second usually wins because the JVM can optimize bounds checks, choose block-copy instructions, and handle memory movement closer to the runtime and hardware.
For object arrays, the JVM may also perform required type and safety checks efficiently while copying references in bulk.
Overlapping Copies and What They Mean
One useful feature of System.arraycopy is that it handles overlapping regions correctly when the source and destination are the same array.
Output:
This still runs in O(n) for the copied segment, but the runtime picks a safe direction for the move so earlier writes do not corrupt later reads.
That behavior makes System.arraycopy more reliable than naive loop code for in-place shifting operations.
What Work Happens Besides the Copy
The method does more than raw memory movement. It validates that:
- both arguments are arrays
- indexes are within range
- '
lengthis not negative' - the source and destination element types are compatible
Those checks add constant overhead, but they do not change the overall complexity. For large copies, the linear copy dominates. For tiny copies, the constant cost may be a noticeable part of runtime, though in practice the method is still often the best choice.
Common Pitfalls
The first mistake is assuming System.arraycopy changes the size of an array. It does not. Java arrays have fixed length, so the destination must already have enough capacity.
Another pitfall is forgetting that object arrays are copied shallowly. The array slots are duplicated, not the objects themselves. After the copy, both arrays still refer to the same underlying objects.
Output:
Developers also misread performance claims and conclude that System.arraycopy is constant time because it is "native." Native implementation changes the constant factors, not the fact that each copied slot still takes work.
Finally, pay attention to type compatibility. Copying from Object[] into String[] may compile but can still fail at runtime with ArrayStoreException if any source element is not actually a String.
Summary
- '
System.arraycopyruns inO(n)time wherenis the number of elements copied.' - It is usually faster than a manual loop because the JVM can optimize it aggressively.
- Overlapping copies are handled safely when source and destination are the same array.
- The method performs bounds and type checks, but those add only constant overhead.
- It is a shallow copy operation, so copying object arrays does not clone the objects inside them.
Related reading
- Time complexity of the Ford-Fulkerson method in a flow network with unit capacity edges
- Time Complexity of the Kruskal Algorithm?
- Time Complexity Of This Code Snippet
- Time Complexity of two for loops
- Time complexity to generate all pairs in an array
- Time cost of training with pytorch DDP with multi-GPUs
- Timed annotation in spring metrics
- Timing out the execution time of a controller/method in Spring

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.