Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Note</strong>: Originally I posted C# code in this answer for purposes of illustration, since C# allows you to pass <code>int</code> parameters by reference with the <code>ref</code> keyword. I've decided to update it with actual legal Java code using the first <a href="http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/mutable/MutableInt.html" rel="noreferrer"><code>MutableInt</code></a> class I found on Google to sort of approximate what <code>ref</code> does in C#. I can't really tell if that helps or hurts the answer. I will say that I personally haven't done all that much Java development; so for all I know there could be much more idiomatic ways to illustrate this point.</p> <hr> <p>Perhaps if we write out a method to do the equivalent of what <code>x++</code> does it will make this clearer.</p> <pre><code>public MutableInt postIncrement(MutableInt x) { int valueBeforeIncrement = x.intValue(); x.add(1); return new MutableInt(valueBeforeIncrement); } </code></pre> <p>Right? Increment the value passed and return the original value: that's the definition of the postincrement operator.</p> <p>Now, let's see how this behavior plays out in your example code:</p> <pre><code>MutableInt x = new MutableInt(); x = postIncrement(x); </code></pre> <p><code>postIncrement(x)</code> does what? Increments <code>x</code>, yes. And then <strong>returns what <code>x</code> <em>was</em> before the increment</strong>. This return value then gets assigned to <code>x</code>.</p> <p>So the order of values assigned to <code>x</code> is 0, then 1, then 0.</p> <p>This might be clearer still if we re-write the above:</p> <pre><code>MutableInt x = new MutableInt(); // x is 0. MutableInt temp = postIncrement(x); // Now x is 1, and temp is 0. x = temp; // Now x is 0 again. </code></pre> <p>Your fixation on the fact that when you replace <code>x</code> on the left side of the above assignment with <code>y</code>, "you can see that it first increments x, and later attributes it to y" strikes me as confused. It is not <code>x</code> that is being assigned to <code>y</code>; it is <strong>the value formerly assigned to <code>x</code></strong>. Really, injecting <code>y</code> makes things no different from the scenario above; we've simply got:</p> <pre><code>MutableInt x = new MutableInt(); // x is 0. MutableInt y = new MutableInt(); // y is 0. MutableInt temp = postIncrement(x); // Now x is 1, and temp is 0. y = temp; // y is still 0. </code></pre> <p>So it's clear: <code>x = x++</code> effectively does not change the value of x. It always causes x to have the values x<sub>0</sub>, then x<sub>0</sub> + 1, and then x<sub>0</sub> again.</p> <hr> <p><strong>Update</strong>: Incidentally, lest you doubt that <code>x</code> ever gets assigned to 1 "between" the increment operation and the assignment in the example above, I've thrown together a quick demo to illustrate that this intermediate value does indeed "exist," though it will never be "seen" on the executing thread.</p> <p>The demo calls <code>x = x++;</code> in a loop while a separate thread continuously prints the value of <code>x</code> to the console.</p> <pre><code>public class Main { public static volatile int x = 0; public static void main(String[] args) { LoopingThread t = new LoopingThread(); System.out.println("Starting background thread..."); t.start(); while (true) { x = x++; } } } class LoopingThread extends Thread { public @Override void run() { while (true) { System.out.println(Main.x); } } } </code></pre> <p>Below is an excerpt of the above program's output. Notice the irregular occurrence of both 1s and 0s.</p> <pre> Starting background thread... 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 1 </pre>
 

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