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;
}
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;
}
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.
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
Except this there is no major difference between these two
For a detail answer you can check this so thread
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.
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;
}
}
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.
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.
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.