java - How is it possible that a variable can be re-declared within a loop? -



java - How is it possible that a variable can be re-declared within a loop? -

this code doesn't work:

public class test { public static void main(string[] args) { int i=3; int i=4; system.out.println(i); } }

then why code work?

public class test { public static void main(string[] args) { for(int a=0;a<9;a++) { int b=a+1; system.out.println(b); } } }

aren't re-declaring b 1 time again , again?

each iteration of loop has own scope, declarations of previous iterations no longer in scope in current iteration.

it's equivalent writing :

{ int b=1; system.out.println(b); } { int b=2; system.out.println(b); } { int b=3; system.out.println(b); } ....

java loops for-loop

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -