Difference between defining variables inside and outside of loops

From a style or performance standpoint, is it better to define variables inside or outside of loops?

For example:

int var;
for (...) {
    var = /*something*/;
    // Code that uses var
}

      

or

for (...) {
    int var = /*something*/;
    // Code that uses var
}

      

If you have an idea of ​​how variable declarations work internally and how one of them might perform better than the other (even if only slightly), please share. And outside of performance, which style is preferred?

+3


source to share


5 answers


Inside

for(int i = 0; i < array.length; i++) {
   final String variable = array[i];
}

      

  • Limits the scope of variables.
  • Variable can be final
  • More readable (possibly)

Outside

String variable;
for(int i = 0; i < array.length; i++) {
   variable = array[i];
}

      

  • The variable is available outside the loop.

For each



for(final String variable : array) {
}

      

  • Only once (source required)
  • Saves a limited amount of variables.
  • Looks awesome.

Performance

Passed the following test. This will take about 30 seconds. The results show that there is no performance difference between defining a variable inside or outside the loop. This is most likely due to compiler optimizations. YMMV.

final double MAX = 1e7;

long start = System.nanoTime();
String var1;
for(double i = 0; i < MAX; i++) {
   var1 = UUID.randomUUID().toString();
}
System.out.println((System.nanoTime() - start) / 1e9);

start = System.nanoTime();
for(double i = 0; i < MAX; i++) {
   final String var2 = UUID.randomUUID().toString();
}
System.out.println((System.nanoTime() - start) / 1e9);

      

Style preference discussion: fooobar.com/questions/35699 / ...

+5


source


You must define loop initialization variables for the loop header, which limits its scope in the loop. If you're concerned about performance, you should define variables in a loop.



Define an outer loop variable only if you use the value of that variable outside of the loop.

+1


source


Ok, it depends on how the variable is used in the first place, if you define a variable inside a loop , you will need to initialize the variable before using the variable for the first time, and every time you start the loop, the variable will be reinitialized to that value.

On the other hand, if you want the value of a variable to persist across different runs in the loop, then you must declare it outside . I don't think performance and style can be the main criteria here.

+1


source


If you declare a variable inside a for loop, you cannot access it outside of the loop.

for (...) {
    int var = /*something*/;
    // Code that uses var
}

      

you cannot access var outside of the loop.

int var;
for(...) {
    var = /*something*/;
    // Code that uses var
}

      

var can be obtained outside of the loop. Any other code in the class can use the var value specified in the for loop.

In the end, it all depends on your requirements.

0


source


The only time you should try to move a variable outside of the loop, even if it has never been used outside of the loop, is if you can reuse the object and not use it new

on every iteration of the loop.

StringBuilder builder = new StringBuilder();
for(...) {
    builder.append(..);
    builder.append(..);

    strings[i] = builder.toString();
    // reset builder, to be used again
    builder.setLength(0);
}

      

This is much more efficient than creating a new StringBuilder every time.

In all other cases, you should prefer to declare variables inside the loop if you can

0


source







All Articles