title: Difference between declaring variables before or in loop?
body: <p>I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference? 
  A <em>(quite pointless)</em> example in Java:</p>
  
  <p><strong>a)</strong> declaration before loop:</p>
  
  <pre><code>double intermediateResult;
  for(int i=0; i &lt; 1000; i++){
      intermediateResult = i;
      System.out.println(intermediateResult);
  }
  </code></pre>
  
  <p><strong>b)</strong> declaration (repeatedly) inside loop:</p>
  
  <pre><code>for(int i=0; i &lt; 1000; i++){
      double intermediateResult = i;
      System.out.println(intermediateResult);
  }
  </code></pre>
  
  <p>Which one is better, <strong>a</strong> or <strong>b</strong>? </p>
  
  <p>I suspect that repeated variable declaration (example <strong>b</strong>) creates more overhead <em>in theory</em>, but that compilers are smart enough so that it doesn't matter. Example <strong>b</strong> has the advantage of being more compact and limiting the scope of the variable to where it is used. Still, I tend to code according example <strong>a</strong>.</p>
  
  <p><strong><em>Edit:</em></strong> I am especially interested in the Java case.</p>