title: Does Arrays.copyOf produce a shallow or a deep copy?
body: <p>There seems to be a lot of confusion and different opinions on this out there ([1] and other sources) on whether <code>Arrays.copyOf</code> will produce a deep or shallow copy.</p>
  
  <p>This test suggests that the copy is deep:</p>
  
  <pre><code>String[] sourceArray = new String[] { "Foo" };
  String[] targetArray = java.util.Arrays.copyOf( sourceArray, 1 );
  
  sourceArray[0] = "Bar";
  
  assertThat( targetArray[0] ).isEqualTo( "Foo" ); // passes
  </code></pre>
  
  <p>This test suggests that the copy is shallow:</p>
  
  <pre><code>String[][] sourceArray = new String[][] { new String[] { "Foo" } };
  String[][] targetArray = java.util.Arrays.copyOf( sourceArray, 1 );
  
  sourceArray[0][0] = "Bar";
  
  assertThat( targetArray[0][0] ).isEqualTo( "Foo" ); // fails
  </code></pre>
  
  <p>Is the solution simply that a deep copy of the top-level dimension is made, but other dimensions are a shallow copy? What is the truth?</p>
  
  <p>[1] <a href="https://stackoverflow.com/questions/1564832/how-do-i-do-a-deep-copy-of-a-2d-array-in-java">How do I do a deep copy of a 2d array in Java?</a></p>