Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I just noticed you referenced <a href="http://javadude.com/articles/passbyvalue.htm" rel="noreferrer">my article</a>.</p> <p>The Java Spec says that everything in Java is pass-by-value. There is no such thing as "pass-by-reference" in Java.</p> <p>The key to understanding this is that something like</p> <pre><code>Dog myDog; </code></pre> <p>is <em>not</em> a Dog; it's actually a <em>pointer</em> to a Dog.</p> <p>What that means, is when you have</p> <pre><code>Dog myDog = new Dog("Rover"); foo(myDog); </code></pre> <p>you're essentially passing the <em>address</em> of the created <code>Dog</code> object to the <code>foo</code> method.</p> <p>(I say essentially because Java pointers aren't direct addresses, but it's easiest to think of them that way)</p> <p>Suppose the <code>Dog</code> object resides at memory address 42. This means we pass 42 to the method.</p> <p>if the Method were defined as</p> <pre><code>public void foo(Dog someDog) { someDog.setName("Max"); // AAA someDog = new Dog("Fifi"); // BBB someDog.setName("Rowlf"); // CCC } </code></pre> <p>let's look at what's happening.</p> <ul> <li>the parameter <code>someDog</code> is set to the value 42</li> <li>at line "AAA" <ul> <li><code>someDog</code> is followed to the <code>Dog</code> it points to (the <code>Dog</code> object at address 42)</li> <li>that <code>Dog</code> (the one at address 42) is asked to change his name to Max</li> </ul></li> <li>at line "BBB" <ul> <li>a new <code>Dog</code> is created. Let's say he's at address 74</li> <li>we assign the parameter <code>someDog</code> to 74 </li> </ul></li> <li>at line "CCC" <ul> <li>someDog is followed to the <code>Dog</code> it points to (the <code>Dog</code> object at address 74)</li> <li>that <code>Dog</code> (the one at address 74) is asked to change his name to Rowlf</li> </ul></li> <li>then, we return</li> </ul> <p>Now let's think about what happens outside the method:</p> <p><em>Did <code>myDog</code> change?</em></p> <p>There's the key.</p> <p>Keeping in mind that <code>myDog</code> is a <em>pointer</em>, and not an actual <code>Dog</code>, the answer is NO. <code>myDog</code> still has the value 42; it's still pointing to the original <code>Dog</code> (but note that because of line "AAA", its name is now "Max" - still the same Dog; <code>myDog</code>'s value has not changed.)</p> <p>It's perfectly valid to <em>follow</em> an address and change what's at the end of it; that does not change the variable, however.</p> <p>Java works exactly like C. You can assign a pointer, pass the pointer to a method, follow the pointer in the method and change the data that was pointed to. However, you cannot change where that pointer points.</p> <p>In C++, Ada, Pascal and other languages that support pass-by-reference, you can actually change the variable that was passed.</p> <p>If Java had pass-by-reference semantics, the <code>foo</code> method we defined above would have changed where <code>myDog</code> was pointing when it assigned <code>someDog</code> on line BBB.</p> <p>Think of reference parameters as being aliases for the variable passed in. When that alias is assigned, so is the variable that was passed in.</p>
 

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