title: Java Array loop behaviour
body: <p>I have been testing some different ways to multiply array items with a constant.</p>
  
  <p>I have produced different results depending on how I loop through the array and I'm having trouble understanding this (I'm fairly new to Java and still getting my head around how things are passed or referenced).</p>
  
  <p><strong>Test 1</strong></p>
  
  <pre><code>int[] array = {1, 2, 3, 4};
  
  for (int number : array)
  {
      number *= 2;
  }
  </code></pre>
  
  <p>Resulting in array items unmodified:</p>
  
  <pre><code>{1, 2, 3, 4}
  </code></pre>
  
  <p>It seems like <code>number</code> is not the actual array item, but a new <code>int</code> that is initialised with it's value. Is that correct?</p>
  
  <hr>
  
  <p><strong>Test 2</strong></p>
  
  <p>I thought that by using an array of Objects might work, assuming <code>number</code> is then a reference to the actual array item. Here is my test of that using <code>Integer</code> instead of <code>int</code>:</p>
  
  <pre><code>Integer[] array = {1, 2, 3, 4};
  
  for (Integer number : array)
  {
      number *= 2;
  }
  </code></pre>
  
  <p>Again, resulting in array items unmodified:</p>
  
  <pre><code>{1, 2, 3, 4}
  </code></pre>
  
  <hr>
  
  <p><strong>Test 3</strong></p>
  
  <p>After some head scratching I tried a different loop method, like this:</p>
  
  <pre><code>int[] array = {1, 2, 3, 4};
  
  for (int i = 0; i &lt; array.length; i ++)
  {
      array[i] *= 2;
  }
  </code></pre>
  
  <p>Resulting in array items multiplied:</p>
  
  <pre><code>{2, 4, 6, 8}
  </code></pre>
  
  <p>This final result makes sense to me, but I don't understand the results from the second test (or first for that matter). Until now, I've always assumed the loop used in test 1 and 2 was just a shorthand version of the loop used in test 3, but they are clearly different.</p>
  
  <p>Why isn't this working as I expected? Why are these loops different?</p>