Quiz yourself: Copying arrays in Java with the java.util.Arrays class

January 3, 2022 | 4 minute read
Text Size 100%:

When copying data, you need to know when you’ve copied a value contained in an object and when you’ve copied a reference to the object.

Download a PDF of this article
More quiz questions available here

Given the class

Copied to Clipboard
Error: Could not Copy
Copied to Clipboard
Error: Could not Copy
import java.util.Arrays;
public class Arr {
  public static void main(String[] args) {
    String[][] src = {{"A"},{"B", "C"},{"D", "E", "F"}};
    String[][] dest = Arrays.copyOf(src, 3); // line n1
    src[2][2]= "X"; // line n2
    System.out.print(src[2].equals(dest[2]));
  }
}

Which is the output? Choose one.

A. true
B. false
C. Runtime exception at line n1
D. Runtime exception at line n2

Answer. This question looks at copying arrays using the Arrays utility class.

The java.util.Arrays class contains utility methods for working with arrays. In particular, there are several overloaded methods used for copying arrays. The code at line n1 correctly performs copying of the src array and assigns the new array reference to dest.

A key question when copying data in a language such as Java is whether you have copied a value contained in an object or the reference to that object. This question becomes more complex when copying is performed on an array of arrays, that is, on a multidimensional array. This complexity is because the values in the first array are themselves references to other arrays.

In the case of the copy method used in this question, the operation will copy the values of the elements of the source array—and only those values.

Of course, in this case, those values are references to other arrays, but (critical to understanding this question) only those reference values are copied. This is commonly called a shallow copy. The effect of the shallow copy is that dest refers to a new array containing the same three (reference) values as the src array. However, because those values are references, that means the subarray objects—that is, {"A"},{"B", "C"} and {"D", "E", "F"}—are shared between the src and dest arrays.

What happens next? In this quiz, line n2 modifies the array {"D", "E", "F"} to become {"D", "E", "X"}. However, note that src[2] and dest[2] still point to the same array, which is now {"D", "E", "X"}.

Consider how the equals method will operate when given src[2] and dest[2] as operands. In this example, using == would return true, since these are both references to the same object.

While it’s common for the equals method to be overridden to return true in situations where == would return false, it’s virtually unheard of for equals to return false when == returns true. After all, how could it make sense for an object to be unequal to itself? This would make it a very safe guess that the code will print true.

If you chose to investigate further, you would discover that in fact the equals method of the array type is not overridden and simply inherits its implementation from Object, so again, you should conclude that the output is true.

From this, it’s clear that option A is correct and the other options are incorrect.

As a side note, the deepEquals, deepHashCode, and deepToString methods in the Arrays class operate differently. They perform their operations recursively on arrays that form the elements of the higher-level array. However, the Arrays class does not provide a comparable method for deep copying. If there were such a method, and if it were used in this example, a change in one of the subarrays would not affect the copy reachable through the other array, because they would be different objects. Thus, in that hypothetical case, false would be printed.

Conclusion.The correct answer is option A.

Related quizzes

Mikalai Zaikin

Mikalai Zaikin is a lead Java developer at IBA Lithuania (part of worldwide IBA Group) and currently located in Vilnius. During his career, Zaikin has helped Oracle with development of Java certification exams, and he has been a technical reviewer of several Java certification books, including three editions of the famous Sun Certified Programmer for Java study guides by Kathy Sierra and Bert Bates.

Simon Roberts

Simon Roberts joined Sun Microsystems in time to teach Sun’s first Java classes in the UK. He created the Sun Certified Java Programmer and Sun Certified Java Developer exams. He wrote several Java certification guides and is currently a freelance educator who publishes recorded and live video training through Pearson InformIT (available direct and through the O’Reilly Safari Books Online service). He remains involved with Oracle’s Java certification projects.


Previous Post

Quiz yourself: The plus + and equals-equals == operators in Java

Simon Roberts | 4 min read

Next Post


Java’s new HTTP Client API, UNIX-domain sockets, and Simple Web Server