3

Is there any significant difference between following code:

for(int i=0;i<100;i++){
   int s =i;
}

and

int s=0;
for(int i=0;i<100;i++){
   s =i;
}
4

7 回答 7

6

Is there any significant difference between following code:

Just the difference of scope of int s. In former, it would not be visible outside the for loop. Whereas, it would be in the later case. As far as best practices is concerned, you should try to minimize the scope of local variables as far as possible. So, if you are not using int s outside the for loop, then it's better to declare it inside only.

于 2013-07-09T16:20:31.410 回答
2

If there is no use of s outside of the loop then I prefer the first one

for(int i=0;i<100;i++){
   String s =i;
}

Because

The scope of local variables should always be the smallest possible

because

  • This makes code easier to understand, debug and refactor.
  • the memory required for these variables in both cases is very small.

Except this there is no major difference between these two

For a detail answer you can check this so thread

于 2013-07-09T16:22:21.590 回答
0

By declaring the int inside of the for loop, its scope is defined by the for loop. Therefore, it will not be available outside of the loop.

Additionally, you're constantly redeclaring and initializing the int in each iteration of the former loop, as opposed to only reinitializing the int in the latter example. If you do not plan to use the variable outside of the loop, then the former approach may make sense.

于 2013-07-09T16:22:35.140 回答
0

The 1st one is better. The programmer knows that the variable won't impact other things in outer scopes.

The compiler also knows that trivially so it can do some optimization, e.g.

void foo()
{

    for(int i=0;i<100;i++)
    {
        String s = ""+i;
    }

    for(int i=0;i<100;i++)
    {
        Long x = i;
    }
}

Just one local stack slot is needed for s and x, since s and x will never be needed at the same time.

It won't be so obvious for compiler, if s and x are in outer scope

void foo()
{
    String s;
    Long x;

    for(int i=0;i<100;i++)
    {
        s = ""+i;
    }

    for(int i=0;i<100;i++)
    {
        x = i;
    }
}
于 2013-07-09T16:26:45.957 回答
0

As others have mentioned, the only functional difference is scope. With the string declared outside the for loop, it is accessible outside the loop. Declared inside the loop, it is only accessible inside the loop.

However, there is just a bit more to it: when you make a variable accessible outside the loop, you imply that it needs to be accessed outside the loop. Therefore, if it is not accessed later in the method, this may raise a question in the mind of the reader of your code as to whether you forgot to use it, or if the implication is incorrect. It is, therefore, more readable if you declare the string so that it has scope limited as much as is possible to when/where it will be used.

于 2013-07-09T16:27:50.153 回答
-2

Only difference I see in both code is, in first example String s will be local to the for loop. In 2nd example, String s can be accessed outside of the loop as well.

于 2013-07-09T16:25:26.180 回答
-2

Not only is s only visible in the loop in the second but much more importantly, a new s is instantiated every run of the loop. You may want this, you may not. There will be a performance cost to new instantiations, but it is safer if you want to make sure it is garbage collected once you leave the loop and you don't try to access it elsewhere.

于 2013-07-09T16:27:28.407 回答