Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are confusing object mutability/immutability with copying of reference values.</p> <p>In these diagrams, <code>[var/index]</code> is a reference variable, and <code>{{an Object}}</code> is an object.</p> <pre><code>String abc[]={"abc"}; String def[]={}; [abc] ------&gt; {{a String[1]}} [0] --------------&gt; {{a String "abc"}} [def] ------&gt; {{a String[0]}} </code></pre> <p>Now you make <code>def</code> reference variable points to the same object as <code>abc</code> reference variable:</p> <pre><code>def=abc; [abc] ------&gt; {{a String[1]}} / [0] --------------&gt; {{a String "abc"}} / [def] ---/ {{a String[0]}} </code></pre> <p>At this point, the array of length zero is unreferenced, and should be garbage-collectable. We can narrow our discussion to the array of length one. Note that a <code>String[]</code> is an array of references. With this next line, you changed what the only element in the length one array points to.</p> <pre><code>def[0]=def[0]+"changed"; [abc] ------&gt; {{a String[1]}} / [0] ---------\ {{a String "abc"}} / \ [def] ---/ \--&gt; {{a String "abcchanged"}} </code></pre> <p>Note that <code>{{a String "abc"}}</code> itself was not mutated. <code>[abc]</code> and <code>[def]</code> now points to the same <code>{{a String[1]}}</code>, which is mutable (i.e. you can make the elements of the array, which are references to <code>String</code> objects, to point to anything).</p> <hr> <blockquote> <p>in order to prevent <code>abc</code> from changed when i changed <code>def</code>, i will have to do <code>def = abc.clone()</code>;</p> </blockquote> <p>Actually, that's not quite accurate. Let's see what happens if you <code>clone()</code> an array of references to a mutable type <code>StringBuilder</code>.</p> <pre><code> StringBuilder[] abc = new StringBuilder[] { new StringBuilder("Hello") }; StringBuilder[] def = abc.clone(); def[0].append(" world!"); System.out.println(abc[0]); // prints "Hello world!" </code></pre> <p>I won't make the diagrams for you this time, but you can easily draw it out on paper. What's happening here is that even though <code>clone()</code> makes a second <code>{{a StringBuilder[1]}}</code> object with its own element (i.e. <code>def != abc</code>), that element is pointing to the same <code>{{a StringBuilder}}</code> object (i.e. <code>def[0] == abc[0]</code>).</p> <hr> <p>In short:</p> <ul> <li>Immutability means that objects of a certain type can not change in any meaningful way to outside observers <ul> <li><code>Integer</code>, <code>String</code>, etc are immutable</li> <li>Generally all value types should be</li> </ul></li> <li>Array objects are mutable <ul> <li>It may be an array of references to immutable types, but the array itself is mutable <ul> <li>Meaning you can set those references to anything you want</li> <li>Also true for array of primitives</li> </ul></li> <li>An immutable array will not be practical</li> </ul></li> <li>References to objects can be shared <ul> <li>If the object is mutable, mutation will be seen through all these references</li> </ul></li> </ul> <p>If you want more in-depth understanding of the issues, I recommend the following:</p> <ul> <li><a href="https://stackoverflow.com/questions/40480/is-java-pass-by-reference">Is Java pass by reference?</a> -- NO!</li> <li><a href="https://stackoverflow.com/questions/1110826/are-java-function-parameters-always-passed-by-value">Are Java function parameters always passed-by-value?</a> -- YES!</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload