Area of ​​local variable in extended for loop

I have a pretty simple question about variable scope.

I am familiar with Enhanced For-Loops, but I do not understand why I have to declare a new variable to save each item. One example might clarify my question:

int[] ar = {1, 2, 3}; int i = 0; for(i : ar) { // this causes an error if I do not declare a new variable: int i // for(int i : ar) // this works fine System.out.println(i); } 

So why should I declare this new variable? After all, i is available inside the for loop. I did not want to use the previous value of i , I just did not want to declare a new variable. (I suggested that for other repeating elements, it could be faster using the same variable).

I guess how Enhanced For-Loops were created, but doesn't that violate the idea of ​​the whole area?

A question arises related to the above behavior. Does the compiler use the same variable for the entire for loop and just update its value or create a new variable for each iteration?

The interesting part is that if I save as an int declaration I (before and inside the for loop), I even get a compiler error

Duplicate local variable i

which makes (at least for me) things a little weirder. Therefore, I cannot use the previous declared variable i inside the for loop, but I also cannot declare a new one inside it with the same name.

+7
java scope foreach
source share
5 answers

So why should I declare this new variable?

Because that’s how syntax is defined.

In the end, I am available inside the for loop.

This semantics. This is not related to the syntax.

I did not want to use the previous value of i, I just did not want to declare a new variable. (I suggested that for other repeating elements, it could be faster using the same variable).

I do not think about performance. Check and measure. But in this case there is nothing to measure, because any working code is faster than any non-working code.

+3
source share

Does this mean that I have a local variable that gets different values ​​or another variable in each loop?

In terms of language, you have a different variable at each iteration. This is why you can write:

 for(final ItemType item: iterable) { … } 

which is of great importance, since you can instantiate an inner class in a loop, referencing the current element. With Java 8, you can also use lambdas and even omit the final modifier, but the semantics are the same: you don't get unexpected results, like in C #.

I suggested that for other repeating elements it could be faster using the same variable

It is nonsense. Until you know what the generated code looks like, you have no idea.

But if you are interested in the details of Java bytecode: inside the stack frame, local variables are addressed by a number, not a name. And the local variables of your program are mapped to these repositories by reusing the repository of local variables that go beyond. It does not matter if the variable exists throughout the cycle or is "recreated" at each iteration. It will still occupy only one slot in the stack frame. Therefore, trying to “reuse local variables” at the source code level makes no sense. It just makes your program less readable.

+2
source share

Just to have a link here: JLS Section 14.14.2, the extended for statement defines the extended for-loop to have the following structure (relates to this question):

  EnhancedForStatement: for ( {VariableModifier} UnannType VariableDeclaratorId : Expression ) Statement 

where UnannType can be summed as a "type" (primitive, reference ...). Thus, specifying the type of the loop variable is mandatory in accordance with the language specification — the reason (admittedly: somewhat confusing) for the observations described in the question.

+1
source share

int i in the program is visible for the for loop and, possibly, for the loops below it (if present) in the same area. But I inside the for(int i : ar) is local to the for loop. Therefore, it ends after the cycle ends. This is the syntax defined for the foreach loop, which "you must use a variable with scope limited by the loop."

 So why I should declare this new variable? After all i is accessible inside the for loop. I did not want to use any previous value of i, just did not want to declare a new variable. (I guessed for other iterable items it might be faster using the same variable). 

Why get some significant performance advantage if you use the same variable a tiny primitive variable many times and create one only when necessary and which is destroyed after the loop ends.

0
source share

I do not think that someone answered the original question, not to mention that this is the syntax. We all know that this is the syntax. The question is, is it logical, why? In the end, you can use the variable defined immediately before the loop as a loop variable if the loop is not an extended loop!

0
source share

All Articles