title: System.arrayCopy() copies object or reference to object?
body: <p>I am having a final class <code>NameAndValue</code>. I copied an array of <code>NameAndValue</code> objects using <code>System.arrayCopy()</code> and when I changed a <code>NameAndValue</code> object in copied array, it gets reflected in the original array.</p>
  <pre class="lang-java prettyprint-override"><code>public final class NameAndValue {
      public String name;
      public String value;
  
      public NameAndValue() { }
  
      public NameAndValue(String name, String value) {
          this.name = name;
          this.value = value;
      }
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>public class Main {
      public static void main(String[] args) {
          NameAndValue[] nv = new NameAndValue[4];
          nv[0] = new NameAndValue(&quot;A&quot;, &quot;1&quot;);
          nv[1] = new NameAndValue(&quot;B&quot;, &quot;2&quot;);
          nv[2] = new NameAndValue(&quot;C&quot;, &quot;3&quot;);
          nv[3] = new NameAndValue(&quot;D&quot;, &quot;4&quot;);
  
          NameAndValue[] nv2 = new NameAndValue[4];
          System.arraycopy(nv, 0, nv2, 0, 2);
          nv2[2] = new NameAndValue(&quot;Y&quot;, &quot;25&quot;);
          nv2[3] = new NameAndValue(&quot;Z&quot;, &quot;26&quot;);
  
          for (int i = 0; i &lt; 2; i++) {
              NameAndValue[] nv3 = new NameAndValue[4];
              System.arraycopy(nv2, 0, nv3, 0, 4);
              nv3[2].value = String.valueOf(i);
              nv3[3].value = String.valueOf(i + 1);
  
              System.out.println(nv2[2].value);
              System.out.println(nv2[3].value);
              System.out.println(&quot;-----------------------&quot;);
          }
      }
  }
  </code></pre>
  <p>I am getting output as:</p>
  <pre><code>0
  1
  -----------------------
  1
  2
  -----------------------
  </code></pre>
  <p>I should have got output as 25 and 26 in all the cases, right?? Why does it get changed??</p>