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;
}
Manish Kumar
  • 10,214
  • 25
  • 77
  • 147
  • 2
    Please, for the love of others, learn to use spaces after semicolons in an if statement. Actually, spaces between variable names, assignment operators, and the assignment values wouldn't be too bad either! And who is teaching that 'for(' is good style? 'for' is a keyword, and you shouldn't make it look like a method call. – Edwin Buck Jul 10 '13 at 03:17

7 Answers7

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • apart of 'scope issue' Is it something that 'int s=i' will reinitialize the s again and again. and if 'int s' is outside it will be avoided. – Manish Kumar Jul 09 '13 at 16:28
  • @Manish. In 2nd case, `s` will be re-initialized in every loop. In first case, you are re-declaring the variable altogether. – Rohit Jain Jul 09 '13 at 16:29
  • so what is the difference between 're-initialized' and 're-declaring' here.will it make some issue if dealing with a large loop – Manish Kumar Jul 09 '13 at 16:33
  • @Manish. Sure the String objects, would be eligible for garbage collection, as soon as the loop ends, but that doesn't guarantee, it will be garbage collected at that very instance. – Rohit Jain Jul 09 '13 at 16:36
  • so in case of 'String' **1.** `String s = value` inside loop or **2** `String s=null` out of loop and `s=value` inside loop which will u prefer. – Manish Kumar Jul 09 '13 at 16:42
  • @Manish. Ah! Sorry, I think, I got it dramatically wrong in my previous comment. It won't make any difference in both cases if you are creating String like - `String s = "value"`, or `s = "value"`, because that won't necessarily create new object on each iteration due to *String Interning*. – Rohit Jain Jul 09 '13 at 16:45
  • So, basically it won't matter in any case. Just keep in mind that if you don't need it outside the loop, declare it inside. – Rohit Jain Jul 09 '13 at 16:46
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

Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • 5
    -1. I'm confused by this - why does declaring `String s` *outside* the `for` loop ("if there is no use of s outside of the loop") result in a *smaller* scope? Isn't this backwards? – Craig Otis Jul 09 '13 at 16:24
  • 1
    @CraigOtis ..oops I Wrote the answer based on first one ... sorry for the mistake... Copy paste problem – stinepike Jul 09 '13 at 16:31
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.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
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;
    }
}
ZhongYu
  • 19,446
  • 5
  • 33
  • 61
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.

atk
  • 9,244
  • 3
  • 32
  • 32
-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.

Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214
-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.

Smalltown2k
  • 811
  • 8
  • 13
  • Nothing is instantiated here, and there will likely be no performance difference between the two snippets. – arshajii Jul 09 '13 at 16:27