Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scope of local variable in enhanced for-loop

I have a rather simple question about variable scope.

I am familiar with the Enhanced For-Loops but I do not get why I should declare a new variable to keep each element. 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 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).

I guess that's how Enhanced For-Loops were built but does not this break the whole scope idea?

There is a question rising from the above behavior. Whether the compiler uses the same variable for the whole for loop and just updates its value or it creates a new variable for each iteration?

An interesting part is that if I keep both declaration of int i (before and inside the for loop) I even get a compiler error about

Duplicate local variable i

which makes (at least for me) things a bit more strange. So I cannot use the previous declared variable i inside the for loop but neither can I declare a new one inside it with the same name.

like image 742
Eypros Avatar asked Jun 03 '14 09:06

Eypros


People also ask

What is the scope of variable in for loop?

The variable is within the scope of the loop. I.e. you need to be within the loop to access it. It's the same as if you declared a variable within a function, only things in the function have access to it.

What is the scope of a local variable?

The scope of a variable is the region of a program in which the variable is visible, i.e., in which it is accessible by its name and can be used. In Java, the scope of a local variable is the body of the method in which it is declared.

What is the scope of variable declared in for loop Java?

In Java variables declared inside a block have a block scope, which means that they're available only inside this block - they're local to it.

What is the scope of a local variable limited to?

Scope of local variable are limited to method. Such a variable is accessible only within the method or block in which it is declared. The local variable's scope starts from the line they are declared and their scope remains there until the closing curly brace of the method comes.


1 Answers

So why I should declare this new variable?

Because that's the way the syntax is defined.

After all i is accessible inside the for loop.

That's semantics. It's irrelevant to syntax.

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).

Don 't guess about performance. Test and measure. But in this case there's nothing to measure, because any working code is faster than any non-working code.

like image 124
user207421 Avatar answered Oct 15 '22 20:10

user207421