title: Why does array[idx++]+="a" increase idx once in Java 8 but twice in Java 9 and 10?
body: <p>For a challenge, <a href="https://stackoverflow.com/users/1682559/kevin-cruijssen">a fellow code golfer</a> <a href="https://codegolf.stackexchange.com/questions/166012/exchange-capitalization/166081?noredirect=1#comment401670_166081">wrote the following code</a>:</p>
  <pre><code>import java.util.*;
  public class Main {
    public static void main(String[] args) {
      int size = 3;
      String[] array = new String[size];
      Arrays.fill(array, &quot;&quot;);
      for (int i = 0; i &lt;= 100;) {
        array[i++ % size] += i + &quot; &quot;;
      }
      for (String element: array) {
        System.out.println(element);
      }
    }
  }
  </code></pre>
  <p><a href="https://tio.run/##TY3BTsMwEETv@YpRJKSEgFXEjZADH8CpxyoH07rVps46sjdFFcq3BzduKafdnXkz2@mTfnaD4W53nGfqB@cFXRTVKGTVY51lw/hlaYut1SHgUxPjJwOuahAtcZwc7dBHr1iLJz5sWmh/COWCAsQCRoPXejn/MV6fo87m@yZym5iPixXUnqwtFuwJeV4mb@98camkGF3Vcbw3eFnF7fYPqXlDVfXALaomMhVy5Ck//bWkpzDW9IblLcXuLetzENMrN4oaIiiWiyta3pumbJrnXw" rel="noreferrer">When running this code in Java 8, we get the following result:</a></p>
  <pre><code>1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100 
  2 5 8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53 56 59 62 65 68 71 74 77 80 83 86 89 92 95 98 101 
  3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 
  </code></pre>
  <p><a href="https://tio.run/##TY3NboMwEITvPMUIqRKU1krUWymHPkBPOUYcnMSJlpgF2UuqqOLZiYPz09Puznwz2@iTfm92x2mitu@coAmCGoSsei2TpB82lrbYWu09fjQx/hLgpnrREsapox3a4GUrccSHdQ3tDj6fUYBYwKjwUc7nP8bpc9DZ/N5FriPzfbW82pO12Yy9IU3z6O07l10rKUQXZRhfFZaLsN3/ITavqSheuEZRBaZAijTmx0dLfApjTWtYPmPs2bI6ezGt6gZRfQDFcnZD82fTmIzTdAE" rel="noreferrer">When running this code in Java 10, we get the following result:</a></p>
  <pre><code>2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 
  2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 
  2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 
  </code></pre>
  <p>The numbering is entirely off using Java 10. So what is happening here? Is it a bug in Java 10?</p>
  <h2>Follow ups from the comments:</h2>
  <ul>
  <li><p>The issue appears when compiled with Java 9 or later (we found it in Java 10). Compiling this code on Java 8, then running in Java 9 or any later version, including Java 11 early access, gives the expected result.</p>
  </li>
  <li><p>This kind of code is non-standard, but is valid according to the spec. It was found by <a href="https://stackoverflow.com/users/1682559/kevin-cruijssen">Kevin Cruijssen</a> in a <a href="https://codegolf.stackexchange.com/questions/166012/exchange-capitalization/166081?noredirect=1#comment401670_166081">discussion in a <strong>golfing challenge</strong></a>, hence the weird use case encountered.</p>
  </li>
  <li><p><a href="https://stackoverflow.com/users/525036/didier-l">Didier L</a> simplified the issue with this much smaller and more understandable code:</p>
  <pre><code>  class Main {
      public static void main(String[] args) {
        String[] array = { &quot;&quot; };
        array[test()] += &quot;a&quot;;
      }
      static int test() {
        System.out.println(&quot;evaluated&quot;);
        return 0;
      }
    }
  </code></pre>
  <p><a href="https://tio.run/##TYxBCoMwFET3PcWQVaRUui8eoasuxcWvBomNUZIfQcSzp6lpoYthYN7wBlroMs3KDt0rxtaQ97iTtthOwByeRrfwTJxqmXSHMTH5YKdtXzcg1/viuAJ/o6MVFTYIgf12wGOrWXmWRYNzBUHiQ/aUr15bRj78hKtnNZZT4HJOajZWCrWQCcSqE0UWO8XBWVyzbI/xDQ" rel="noreferrer">Result when compiled in Java 8:</a></p>
  <pre><code>  evaluated
  </code></pre>
  <p><a href="https://tio.run/##TYxBCoMwFET3PcWQlaFUui8eoasuxcWvBomNUZIfQcSzp6lpoYthYN7wBlroMnSvGFtD3uNO2mI7AXN4Gt3CM3GqZdIdxsSKBztt@7oBud7L4wr8jY5WVNggBPbbAY@tZuW5kA3OFQSJD9lTvnptGfnwE66e1VhOgcs5qdnYQqiFTCBWnZBZ7BQHZ3HNsj3GNw" rel="noreferrer">Result when compiled in Java 9 and 10:</a></p>
  <pre><code>  evaluated
    evaluated
  </code></pre>
  </li>
  <li><p>The issue seems to be limited to the string concatenation and assignment operator (<code>+=</code>) with an expression with side effect(s) as the left operand, like in <code>array[test()]+=&quot;a&quot;</code>, <code>array[ix++]+=&quot;a&quot;</code>, <code>test()[index]+=&quot;a&quot;</code>, or <code>test().field+=&quot;a&quot;</code>. To enable string concatenation, at least one of the sides must have type <code>String</code>. Trying to reproduce this on other types or constructs failed.</p>
  </li>
  </ul>