Declaring Variables in a Loop

I have a very simple question about coverage rules. When you declare a variable in a loop, say:

while ( /*some condition*/ )
{
  int a = 0;
  //Remaining operations
}

Is a new variable intdeclared at each iteration of the loop? Or is it that is adestroyed at the end of each iteration and is created again? How does the compiler in Java or C ++ understand and implement this?

+4
source share
5 answers

You must distinguish between logical level and implementation level.

"" "", , , , . , ( ), ( 0), . . ++, (.. =0), , ( " " ). Java, , , a ​​ , .

, , , . , , , a. , 0 . , a , . , , CPU.

"" a ( StackOverflow (!) ...). , ( ...).

+3

a , .

+2

, a - , 0, , , .

:

 while(//Some Condition) 

,

, :

while(/* some condition */)
+2

It is declared only in source code. In bytecode, it simply uses a local variable on the stack, which will be initialized from 0 at each iteration. The difference with the declaration outside the loop is that when it is inside the loop, the JVM will reuse the variable that is abusy.

+1
source

a create and destroy after each iteration.

+1
source

All Articles