title: Is there a difference in runtime efficiency if I evaluate the size of the array outside the loop?
body: <p>The traditional way to iterate over an (integer, in this example) array of elements is the following:</p>
  
  <pre><code>int[] array = {5, 10, 15};
  
  for(int i = 0; i &lt; array.length; i++) [
      //do something with array[i]
  }
  </code></pre>
  
  <p>However, does this mean that after each iteration 'array.length' is re-evaluated? Would it not be more efficient to do this? :</p>
  
  <pre><code>int[] array = {5, 10, 15};
  
  int noOfElements = array.length;
  
  for(int i = 0; i &lt; noOfElements; i++) {
      //do something with array[i]
  }
  </code></pre>
  
  <p>In this way, (to my understanding) the program only has to calculate it once and then look up the value of 'noOfElements' variable.</p>
  
  <p><em>Note</em>: I am aware of the enhanced for-loop, but it cannot be used when you want to use the variable that is being incremented ('i' in this example) to achieve other things within the for-loop.</p>
  
  <p>I'm suspecting that this is actually a question of whether the Java compiler has the capability of realising that 'array.length' doesn't change and actually reusing that value after calculating it once.</p>
  
  <p>So my question is: <b>Is there a difference in runtime efficiency of the first block of code I wrote and the second one?</b></p>
  
  <p>What I gather from the replies below, is that when an array is instantiated (is that the right word?) an instance variable called length is created and it is equal to the number of elements in the array.</p>
  
  <p>What this means is that the statement array.length has nothing to do with calculation; it is only referencing the instance variable.</p>
  
  <p>Thanks for the input guys!</p>