Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in coding style [duplicate]

Tags:

java

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;
}
like image 742
Manish Kumar Avatar asked Jan 21 '26 14:01

Manish Kumar


1 Answers

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.

like image 96
Rohit Jain Avatar answered Jan 24 '26 04:01

Rohit Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!