Method System.arraycopy() - Quiz
Total: 5 questions
1. What does System.arraycopy() do and what are its parameters?
What does System.arraycopy() do and what are its parameters?
System.arraycopy() copies a given number of elements from one array into another (or into the same array). It is a static method of the java.lang.System class with the signature arraycopy(Object src, int srcPos, Object dest, int destPos, int length): src is the source array, srcPos the index in the source where copying starts, dest the destination array, destPos the index where elements are written in the destination, and length the number of elements to copy. The method returns nothing (void) and does not create a new array — the destination must already exist and be long enough before the call.
2. Which exceptions does System.arraycopy() throw, and when?
Which exceptions does System.arraycopy() throw, and when?
NullPointerException — when src or dest is null. ArrayIndexOutOfBoundsException — when srcPos, destPos, or length is negative, or the range srcPos + length / destPos + length goes past the end of the corresponding array. ArrayStoreException — when an element from src cannot be stored in dest because of a type mismatch. Note: if some elements have already been transferred before the exception is thrown, those changes in dest remain — the method does not roll back the part it has already completed.
3. How does System.arraycopy() differ from Arrays.copyOf() and clone()?
How does System.arraycopy() differ from Arrays.copyOf() and clone()?
System.arraycopy() does not create a new array: it copies part of (or the whole) array into an existing destination, including overlapping ranges. Arrays.copyOf(array, newLength) does create a new array of the requested length and copies the elements, and it uses System.arraycopy() internally. array.clone() also creates a new array — a full shallow copy of the same length and type. Bottom line: if the destination already exists and you need only part of the data, use System.arraycopy(); if you need a brand-new array, Arrays.copyOf() or clone() is shorter.
4. Can you copy an array into itself when the source and destination ranges overlap?
Can you copy an array into itself when the source and destination ranges overlap?
Yes, and it is safe even with overlapping ranges. System.arraycopy() behaves as if the elements were first copied into a temporary buffer, so they never overwrite each other too early. For example, for {1, 2, 3, 4, 5, 6, 7, 8} the call System.arraycopy(array, 1, array, 3, 3) produces [1, 2, 3, 2, 3, 4, 7, 8]. A naive for loop that copies elements one by one in increasing index order would give a wrong result on the same overlap — it would clobber values before they were ever read.
5. Why is System.arraycopy() faster than a hand-written for loop?
Why is System.arraycopy() faster than a hand-written for loop?
The method is declared native — its body is implemented at the JVM level, not in Java. The JVM performs the copy directly in memory (similar to low-level operations such as memmove), skipping the overhead of regular bytecode: per-iteration bounds checks, counter increments, and method calls. That is why Arrays.copyOf(), Arrays.copyOfRange(), and the internals of many collections are built on it. On small arrays the difference from a loop is negligible, but on tens of thousands of elements and more System.arraycopy() is noticeably faster and safer.