Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Possible Duplicate:
Where do you declare variables? The top of a method or when you need them?

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)

share|improve this question
    
I'm pleased you actually looked at the disassembly and hope it taught you something. I had hoped someone with actual Java experience would answer your final question about the optimized code, but perhaps you can post that specific part over on Stackoverflow - it seems to be a very concrete question. –  Joris Timmermans Oct 8 '12 at 15:41
    
Yes, I will try to get the optimized code. (The question changed a bit, I asked the thing with the optimized code in the edit) –  Puckl Oct 8 '12 at 15:49
add comment

marked as duplicate by Jalayn, Joachim Sauer, Puckl, Péter Török, Walter Oct 8 '12 at 13:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

At the level of the individual variable there is no significant difference in effeciency, but if you had a function with 1000 loops and 1000 variables (never mind the bad style implied) there could be systemic differences because all the lives of all the variables would be the same instead of overlapped. This could affect things like stack size and the garbage collector's ability to clean up the variables that were being kept alive longer than necessary.

Also, it's much better style to give variables the smallest possible scope. It prevents accidents.

share|improve this answer
    
This is not true - at the JVM level, there is no such thing as limited scope for local variables. –  Michael Borgwardt Oct 8 '12 at 9:46
    
do you mean that if I write for(int i= ..) 1000 times, there will be 1000 different variables on the stack when I exit the function? –  ddyer Oct 8 '12 at 9:55
    
according to artima.com/insidejvm/ed2/jvm8.html "For example, if two local variables have limited scopes that don't overlap, such as the i and j local variables in Example3b, compilers are free to use the same array entry for both variables. " And that only makes sense, compilers are free to optimize the size of the stack frame in this way. –  ddyer Oct 8 '12 at 10:09
1  
Good point; but if we're talking about compiler optimizations, the compiler can almost as easily reuse local variable entries for variables that have overlapping lexical scope but are not used in an overlapping manner. –  Michael Borgwardt Oct 8 '12 at 10:43
add comment

Short answer : no. There were similar questions somewhere on this site already. There is no notable difference as per the generated bytecode. Declaring them when needed produces less lines of code

Here is the accepted answer : http://programmers.stackexchange.com/a/56590/43451

share|improve this answer
    
What about the performance when you create a new object inside the loop, that could just as well be created only once? –  Bubblewrap Oct 8 '12 at 12:59
3  
In this case obviously there is performance and memory penalty. But it is not quite the case in the op's question –  Kemoda Oct 8 '12 at 13:27
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.