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

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

Is this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for(int i = 0; i < 1000; i++) {
int temp = doSomething();
someMethod(temp);
}
</code>
<code>for(int i = 0; i < 1000; i++) { int temp = doSomething(); someMethod(temp); } </code>
for(int i = 0; i < 1000; i++) {
   int temp = doSomething();
   someMethod(temp);
}

equal to this (with respect to memory usage)?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>int temp = 0;
for(int i = 0; i < 1000; i++) {
temp = doSomething();
someMethod(temp);
}
</code>
<code>int temp = 0; for(int i = 0; i < 1000; i++) { temp = doSomething(); someMethod(temp); } </code>
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?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for(int i = 0; i < 1000; i++) {
ArrayList<Integer> array = new ArrayList<Integer>();
fillArray(array);
// do something with the array
}
</code>
<code>for(int i = 0; i < 1000; i++) { ArrayList<Integer> array = new ArrayList<Integer>(); fillArray(array); // do something with the array } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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
</code>
<code> 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 </code>
  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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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
</code>
<code> 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 </code>
  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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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++;
}
}
</code>
<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++; } } </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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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
</code>
<code> 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 </code>
  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)

3

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.

0

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.

4

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.

4

Who cares about memory usage for a single variable of type int? Nobody. But there is one huge difference if your code is a bit more complicated:

If there are situations where the code inside the loop misses initialising temp then it will without any warning use the value of the previous iteration. If this happens only rarely then you have a very hard to find bug. If you declare temp inside the loop, and the code inside the loop misses initialising temp, then the compiler will tell you about your error.

Always write your code so that the compiler has a chance to find stupid mistakes that you make. A corrolary is that you don’t initialise a variable until you have the actual value that should be stored, and not a placeholder like 0.

If you do a 1000-2000 loop using the two methods, you will find out that declaring it outside is more economical and better optimized. Reason being, re-initialization of the variable in the loop leads to creation of memory location (different ones) for the variable. The extra workload and time lag that is given to the code for the reallocation of memory for variable during the loop traversing leads to a difference in execution time.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật