title: Can the JVM optimize multiple reads from an array
body: <p>Can the JVM optimize array reads when reading the same index multiple times? Consider the following:</p>
  
  <pre><code>public static void foo(int[] array) {
     for (int i=0; i&lt;array.length; i++) {
         int value1 = array[i];
         ...
         int value2 = array[i];
         ...
      }
      ...
  }
  </code></pre>
  
  <p><code>array[i]</code> is read twice in the loop. Assuming <code>array[i]</code> is not reassigned in the loop, is the JVM allowed to assume that <code>array[i]</code> did not change and thus read its value just once? Since <code>array</code> is a passed-in mutable object, it could conceivably have changed between the first and second read.</p>
  
  <p>I've looked the at the generated byte code and it does indeed read <code>array[i]</code> twice (<code>daload</code>). Is the JVM allowed to optimize this into one read?</p>