Possible Duplicate:
Difference between declaring variables before or in loop?
Consider the two codes below :
Object i;
for(int i=0;i<10;i++){
i = new Object();
}
OR
for(int i=0;i<10;i++){
Object i = new Object();
}
which of the two codes above is better at performance and buffer-overflow.
I like to limit variable scope as much as possible. The first option scopes the variable to the entire containing function, while the latter limits it to just within the loop. Therefore, I prefer the latter unless I explicitly need access to the variable after the loop completes.