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
Post a Comment