Java – Declaring Variables Inside or Outside a Loop

javaloopsvariables

Does it make a difference if I declare variables inside or outside a loop in Java?

Is this

for(int i = 0; i < 1000; i++) {
   int temp = doSomething();
   someMethod(temp);
}

equal to this (with respect to memory usage)?

int temp = 0;
for(int i = 0; i < 1000; i++) {
   temp = doSomething();
   someMethod(temp);
}

And what if the temporary variable is for example an ArrayList?

for(int i = 0; i < 1000; i++) {
   ArrayList<Integer> array = new ArrayList<Integer>();
   fillArray(array);
   // do something with the array
}

EDIT: with javap -c I got the following output

Variable outside the loop:

  public static void main(java.lang.String[]);
    Code:
       0: iconst_0      
       1: istore_1      
       2: iconst_0      
       3: istore_2      
       4: iload_2       
       5: sipush        1000
       8: if_icmpge     25
      11: invokestatic  #2                  // Method doSomething:()I
      14: istore_1      
      15: iload_1       
      16: invokestatic  #3                  // Method someMethod:(I)V
      19: iinc          2, 1
      22: goto          4
      25: return  

Variable inside the loop:

  public static void main(java.lang.String[]);
    Code:
       0: iconst_0      
       1: istore_1      
       2: iload_1       
       3: sipush        1000
       6: if_icmpge     23
       9: invokestatic  #2                  // Method doSomething:()I
      12: istore_2      
      13: iload_2       
      14: invokestatic  #3                  // Method someMethod:(I)V
      17: iinc          1, 1
      20: goto          2
      23: return        

And for the interested, this code:

public class Test3 {
    public static void main(String[] args) {
        for(int i = 0; i< 1000; i++) {
            someMethod(doSomething());
        }   
    }
    private static int doSomething() {
        return 1;
    }
    private static void someMethod(int temp) {
        temp++;
    }
}

produces this:

  public static void main(java.lang.String[]);
    Code:
       0: iconst_0      
       1: istore_1      
       2: iload_1       
       3: sipush        1000
       6: if_icmpge     21
       9: invokestatic  #2                  // Method doSomething:()I
      12: invokestatic  #3                  // Method someMethod:(I)V
      15: iinc          1, 1
      18: goto          2
      21: return   

But the optimization happens at runtime then. Is there a way to look at the optimized code? (Sorry for the long EDIT)

Best Answer

The common answer to most of these questions should be "why don't you try it and find out?". In Java you could probably take a look at the generated bytecode (I believe the tool is called javap), to see what the difference in byte code is between those two ways of declaring the variable.

Doing it like that is a better learning experience for you, because next time you're running into an optimization issue you can use the same tool to verify that the compiler is doing what you are expecting - it will help you avoid needlessly changing your coding style when the optimizer does fine on its own, or finding actual tweaks when you really need that last bit of performance.

Related Topic