Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think there is a slight misunderstanding from the background text:</p> <blockquote> <p>In Java, when we pass a java object argument to method myMethod((MyObject) myObject), it is basically like passing a reference to object in C++ like myFunction(&amp; myObject).</p> </blockquote> <p>Java is <strong>not</strong> a "pass-by-reference" language. It always passes by value. When you pass an object, you are passing a reference to that object <em>by value</em>. This is analogous in C++ when you pass a pointer by value. </p> <p>For example, in Java:</p> <pre><code>public void myMethod(MyObject myObject) { myObject = null; } </code></pre> <p>The object passed as "MyObject" is only a reference by value. So setting it to null does not affect the original object that was passed. Correspondingly:</p> <pre><code>public void myMethod(MyObject myObject) { myObject.myIntProperty = 15; } </code></pre> <p>Accessing members of the object, as above, does so to the actual object, since it is still a reference to that object. If it helps, think of the passed in <code>myObject</code> as having a copy of the memory location that is the same as the memory location of the original object that was passed:</p> <pre><code>public void myMethod(MyObject myObject) {} /* Example memory address: 0x009FC12A in memory, a *copy* of the same address of the object that was passed */ </code></pre> <hr> <p>With this mindset, let's take a look at your question. What does this yield:</p> <pre><code>myObjectsFour[0] = myObjectsOne[0]; </code></pre> <p>Well, as Java assigns the reference of the element <em>by value</em>, <code>myObjectsFour[0]</code> now points to precisely the same object. This means:</p> <pre><code>myObjectsFour[0] = myObjectsOne[0]; myObjectsFour[0].myIntProperty = 15; </code></pre> <p>Also changes the value of <code>myObjectsOne[0].myIntProperty</code> to be 15, just as above. However doing this:</p> <pre><code>myObjectsFour[0] = myObjectsOne[0]; myObjectsFour[0] = null; // (or anything else, for that matter) </code></pre> <p>has no affect on <code>myObjectsOne[0]</code>. It's not the <em>same</em> variable by the statement, <code>myObjectsFour[0] = myObjectsOne[0];</code>. It's simply two variables that have a copy of the same reference. </p> <p>Does that make sense?</p>
    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