Enhanced for loop and lambda expressions












7















To my understanding, lambda expressions capture values, not variables. For example, the following is a compile-time error:



for (int k = 0; k < 10; k++) {
new Thread(() -> System.out.println(k)).start();
// Error—cannot capture k
//Local variable k defined in an enclosing scope must be final or effectively final
}


however when I try to ran the same logic with enhanced for-loop every thing is working fine



ArrayList<Integer> listOfInt = new ArrayList<Integer>() {
{
add(1);
add(2);
add(3);
}
};

for (Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
// OK to capture arg
}


Please help me understand why it is working fine for enhanced for loop and not for normal loop, although enhanced for loop also somewhere inside incrementing the variable as done by normal loop.










share|improve this question

























  • // Error—cannot capture i Did you mean k rather than i?

    – Prashant Zombade
    1 hour ago











  • @PrashantZombade , typo please check the question again.

    – Show Stopper
    1 hour ago
















7















To my understanding, lambda expressions capture values, not variables. For example, the following is a compile-time error:



for (int k = 0; k < 10; k++) {
new Thread(() -> System.out.println(k)).start();
// Error—cannot capture k
//Local variable k defined in an enclosing scope must be final or effectively final
}


however when I try to ran the same logic with enhanced for-loop every thing is working fine



ArrayList<Integer> listOfInt = new ArrayList<Integer>() {
{
add(1);
add(2);
add(3);
}
};

for (Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
// OK to capture arg
}


Please help me understand why it is working fine for enhanced for loop and not for normal loop, although enhanced for loop also somewhere inside incrementing the variable as done by normal loop.










share|improve this question

























  • // Error—cannot capture i Did you mean k rather than i?

    – Prashant Zombade
    1 hour ago











  • @PrashantZombade , typo please check the question again.

    – Show Stopper
    1 hour ago














7












7








7


3






To my understanding, lambda expressions capture values, not variables. For example, the following is a compile-time error:



for (int k = 0; k < 10; k++) {
new Thread(() -> System.out.println(k)).start();
// Error—cannot capture k
//Local variable k defined in an enclosing scope must be final or effectively final
}


however when I try to ran the same logic with enhanced for-loop every thing is working fine



ArrayList<Integer> listOfInt = new ArrayList<Integer>() {
{
add(1);
add(2);
add(3);
}
};

for (Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
// OK to capture arg
}


Please help me understand why it is working fine for enhanced for loop and not for normal loop, although enhanced for loop also somewhere inside incrementing the variable as done by normal loop.










share|improve this question
















To my understanding, lambda expressions capture values, not variables. For example, the following is a compile-time error:



for (int k = 0; k < 10; k++) {
new Thread(() -> System.out.println(k)).start();
// Error—cannot capture k
//Local variable k defined in an enclosing scope must be final or effectively final
}


however when I try to ran the same logic with enhanced for-loop every thing is working fine



ArrayList<Integer> listOfInt = new ArrayList<Integer>() {
{
add(1);
add(2);
add(3);
}
};

for (Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
// OK to capture arg
}


Please help me understand why it is working fine for enhanced for loop and not for normal loop, although enhanced for loop also somewhere inside incrementing the variable as done by normal loop.







java lambda






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







Show Stopper

















asked 1 hour ago









Show StopperShow Stopper

5,1751965




5,1751965













  • // Error—cannot capture i Did you mean k rather than i?

    – Prashant Zombade
    1 hour ago











  • @PrashantZombade , typo please check the question again.

    – Show Stopper
    1 hour ago



















  • // Error—cannot capture i Did you mean k rather than i?

    – Prashant Zombade
    1 hour ago











  • @PrashantZombade , typo please check the question again.

    – Show Stopper
    1 hour ago

















// Error—cannot capture i Did you mean k rather than i?

– Prashant Zombade
1 hour ago





// Error—cannot capture i Did you mean k rather than i?

– Prashant Zombade
1 hour ago













@PrashantZombade , typo please check the question again.

– Show Stopper
1 hour ago





@PrashantZombade , typo please check the question again.

– Show Stopper
1 hour ago












3 Answers
3






active

oldest

votes


















4














Lambda-Expressions work like callbacks. The moment they are passed in the code, they 'store' any external values (or references) they require to operate (as if these values were passed as arguments in a function call. This is just hidden from the developer). In your first example, you could work around the problem by storing k to a separate variable, like d:



for (int k = 0; k < 10; k++) {
final int d = k
new Thread(() -> System.out.println(d)).start();
}


Effectively final means, that in the above example, you can leave the 'final' keyword out, because d is effectively final, since it is never changed within it's scope.



for-loops operate differently. They are iterative code (as opposed to a callback). They work within their respective scope and can use all variables on their own stack. This means, that the for-loop's code block is part of the external code block.



As to your highlighted question:
An enhanced for-loop does not operate with a regular index-counter, at least not directly. Enhanced for loops (over non-arrays) create a hidden Iterator. You can test this the following way:



Collection<String> mySet = new HashSet<>();
mySet.addAll(Arrays.asList("A", "B", "C"));
for (String myString : mySet) {
if (myString.equals("B")) {
mySet.remove(myString);
}
}


The above example will cause a ConcurrentModificationException. This is due to the iterator noticing, that the underlying collection has changed during the execution. However in your very example, the external loop creates an 'effectively final' variable arg which can be referenced within the lambda expression, because the value is captured at execution time.
The prevention of the capture of 'non-effectively-final' values is more or less just a precaution in Java, because in other languages (like JavaScript e.g.) this works differently. So the compiler could theoretically translate your code, capture the value, and continue, but it would have to store that value differently, and you would probably get unexpected results. Therefore the team developing lambdas for Java 8 correctly excluded this scenario, by preventing it with an exception.



If you ever need to change values of external variables within lambda expressions, you can either declare a one-element array:



String myStringRef = { "before" };
someCallingMethod(() -> myStringRef[0] = "after" );
System.out.println(myStringRef[0]);


Or use Atomic to make it thread-safe. However with your example, this would probably return "before" since the thread would most likely execute after the execution of println.






share|improve this answer

































    2














    In an enhanced for loop the variable is initialized for each iteration. From §14.14.2 of the JLS:




    ...



    When an enhanced for statement is executed, the local variable is initialized, on each iteration of the loop, to successive elements of the array or Iterable produced by the expression. The precise meaning of the enhanced for statement is given by translation into a basic for statement, as follows:





    • If the type of Expression is a subtype of Iterable, then the translation is as follows.



      If the type of Expression is a subtype of Iterable for some type argument X, then let I be the type java.util.Iterator; otherwise, let I be the raw type java.util.Iterator.



      The enhanced for statement is equivalent to a basic for statement of the form:



      for (I #i = Expression.iterator(); #i.hasNext(); ) {
      {VariableModifier} TargetType Identifier =
      (TargetType) #i.next();
      Statement
      }



    ...





    • Otherwise, the Expression necessarily has an array type, T.



      Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement.



      The enhanced for statement is equivalent to a basic for statement of the form:



      T #a = Expression;
      L1: L2: ... Lm:
      for (int #i = 0; #i < #a.length; #i++) {
      {VariableModifier} TargetType Identifier = #a[#i];
      Statement
      }



    ...




    In other words, your enhanced for loop is equivalent to:



    ArrayList<Integer> listOfInt = new ArrayList<>();
    // add elements...

    for (Iterator<Integer> itr = listOfInt.iterator(); itr.hasNext(); ) {
    Integer arg = itr.next();
    new Thread(() -> System.out.println(arg)).start();
    }


    Since the variable is initialized each iteration it is effectively final (unless you modify the variable inside the loop).



    In contrast, the variable in the basic for loop (k in your case) is initialized once and updated each iteration (if a "ForUpdate" is present, e.g. k++). See §14.14.1 of the JLS for more information. Since the variable is updated each iteration is is not final nor effectively final.






    share|improve this answer

































      0














      An enhanced for loop is defined to be equivalent to this code:



      for (Iterator<T> it = iterable.iterator(); it.hasNext(); ) {
      T loopvar = it.next();

      }


      This substitution code explains why the variable of an enhanced for loop is considered effectively final.






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54340101%2fenhanced-for-loop-and-lambda-expressions%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        Lambda-Expressions work like callbacks. The moment they are passed in the code, they 'store' any external values (or references) they require to operate (as if these values were passed as arguments in a function call. This is just hidden from the developer). In your first example, you could work around the problem by storing k to a separate variable, like d:



        for (int k = 0; k < 10; k++) {
        final int d = k
        new Thread(() -> System.out.println(d)).start();
        }


        Effectively final means, that in the above example, you can leave the 'final' keyword out, because d is effectively final, since it is never changed within it's scope.



        for-loops operate differently. They are iterative code (as opposed to a callback). They work within their respective scope and can use all variables on their own stack. This means, that the for-loop's code block is part of the external code block.



        As to your highlighted question:
        An enhanced for-loop does not operate with a regular index-counter, at least not directly. Enhanced for loops (over non-arrays) create a hidden Iterator. You can test this the following way:



        Collection<String> mySet = new HashSet<>();
        mySet.addAll(Arrays.asList("A", "B", "C"));
        for (String myString : mySet) {
        if (myString.equals("B")) {
        mySet.remove(myString);
        }
        }


        The above example will cause a ConcurrentModificationException. This is due to the iterator noticing, that the underlying collection has changed during the execution. However in your very example, the external loop creates an 'effectively final' variable arg which can be referenced within the lambda expression, because the value is captured at execution time.
        The prevention of the capture of 'non-effectively-final' values is more or less just a precaution in Java, because in other languages (like JavaScript e.g.) this works differently. So the compiler could theoretically translate your code, capture the value, and continue, but it would have to store that value differently, and you would probably get unexpected results. Therefore the team developing lambdas for Java 8 correctly excluded this scenario, by preventing it with an exception.



        If you ever need to change values of external variables within lambda expressions, you can either declare a one-element array:



        String myStringRef = { "before" };
        someCallingMethod(() -> myStringRef[0] = "after" );
        System.out.println(myStringRef[0]);


        Or use Atomic to make it thread-safe. However with your example, this would probably return "before" since the thread would most likely execute after the execution of println.






        share|improve this answer






























          4














          Lambda-Expressions work like callbacks. The moment they are passed in the code, they 'store' any external values (or references) they require to operate (as if these values were passed as arguments in a function call. This is just hidden from the developer). In your first example, you could work around the problem by storing k to a separate variable, like d:



          for (int k = 0; k < 10; k++) {
          final int d = k
          new Thread(() -> System.out.println(d)).start();
          }


          Effectively final means, that in the above example, you can leave the 'final' keyword out, because d is effectively final, since it is never changed within it's scope.



          for-loops operate differently. They are iterative code (as opposed to a callback). They work within their respective scope and can use all variables on their own stack. This means, that the for-loop's code block is part of the external code block.



          As to your highlighted question:
          An enhanced for-loop does not operate with a regular index-counter, at least not directly. Enhanced for loops (over non-arrays) create a hidden Iterator. You can test this the following way:



          Collection<String> mySet = new HashSet<>();
          mySet.addAll(Arrays.asList("A", "B", "C"));
          for (String myString : mySet) {
          if (myString.equals("B")) {
          mySet.remove(myString);
          }
          }


          The above example will cause a ConcurrentModificationException. This is due to the iterator noticing, that the underlying collection has changed during the execution. However in your very example, the external loop creates an 'effectively final' variable arg which can be referenced within the lambda expression, because the value is captured at execution time.
          The prevention of the capture of 'non-effectively-final' values is more or less just a precaution in Java, because in other languages (like JavaScript e.g.) this works differently. So the compiler could theoretically translate your code, capture the value, and continue, but it would have to store that value differently, and you would probably get unexpected results. Therefore the team developing lambdas for Java 8 correctly excluded this scenario, by preventing it with an exception.



          If you ever need to change values of external variables within lambda expressions, you can either declare a one-element array:



          String myStringRef = { "before" };
          someCallingMethod(() -> myStringRef[0] = "after" );
          System.out.println(myStringRef[0]);


          Or use Atomic to make it thread-safe. However with your example, this would probably return "before" since the thread would most likely execute after the execution of println.






          share|improve this answer




























            4












            4








            4







            Lambda-Expressions work like callbacks. The moment they are passed in the code, they 'store' any external values (or references) they require to operate (as if these values were passed as arguments in a function call. This is just hidden from the developer). In your first example, you could work around the problem by storing k to a separate variable, like d:



            for (int k = 0; k < 10; k++) {
            final int d = k
            new Thread(() -> System.out.println(d)).start();
            }


            Effectively final means, that in the above example, you can leave the 'final' keyword out, because d is effectively final, since it is never changed within it's scope.



            for-loops operate differently. They are iterative code (as opposed to a callback). They work within their respective scope and can use all variables on their own stack. This means, that the for-loop's code block is part of the external code block.



            As to your highlighted question:
            An enhanced for-loop does not operate with a regular index-counter, at least not directly. Enhanced for loops (over non-arrays) create a hidden Iterator. You can test this the following way:



            Collection<String> mySet = new HashSet<>();
            mySet.addAll(Arrays.asList("A", "B", "C"));
            for (String myString : mySet) {
            if (myString.equals("B")) {
            mySet.remove(myString);
            }
            }


            The above example will cause a ConcurrentModificationException. This is due to the iterator noticing, that the underlying collection has changed during the execution. However in your very example, the external loop creates an 'effectively final' variable arg which can be referenced within the lambda expression, because the value is captured at execution time.
            The prevention of the capture of 'non-effectively-final' values is more or less just a precaution in Java, because in other languages (like JavaScript e.g.) this works differently. So the compiler could theoretically translate your code, capture the value, and continue, but it would have to store that value differently, and you would probably get unexpected results. Therefore the team developing lambdas for Java 8 correctly excluded this scenario, by preventing it with an exception.



            If you ever need to change values of external variables within lambda expressions, you can either declare a one-element array:



            String myStringRef = { "before" };
            someCallingMethod(() -> myStringRef[0] = "after" );
            System.out.println(myStringRef[0]);


            Or use Atomic to make it thread-safe. However with your example, this would probably return "before" since the thread would most likely execute after the execution of println.






            share|improve this answer















            Lambda-Expressions work like callbacks. The moment they are passed in the code, they 'store' any external values (or references) they require to operate (as if these values were passed as arguments in a function call. This is just hidden from the developer). In your first example, you could work around the problem by storing k to a separate variable, like d:



            for (int k = 0; k < 10; k++) {
            final int d = k
            new Thread(() -> System.out.println(d)).start();
            }


            Effectively final means, that in the above example, you can leave the 'final' keyword out, because d is effectively final, since it is never changed within it's scope.



            for-loops operate differently. They are iterative code (as opposed to a callback). They work within their respective scope and can use all variables on their own stack. This means, that the for-loop's code block is part of the external code block.



            As to your highlighted question:
            An enhanced for-loop does not operate with a regular index-counter, at least not directly. Enhanced for loops (over non-arrays) create a hidden Iterator. You can test this the following way:



            Collection<String> mySet = new HashSet<>();
            mySet.addAll(Arrays.asList("A", "B", "C"));
            for (String myString : mySet) {
            if (myString.equals("B")) {
            mySet.remove(myString);
            }
            }


            The above example will cause a ConcurrentModificationException. This is due to the iterator noticing, that the underlying collection has changed during the execution. However in your very example, the external loop creates an 'effectively final' variable arg which can be referenced within the lambda expression, because the value is captured at execution time.
            The prevention of the capture of 'non-effectively-final' values is more or less just a precaution in Java, because in other languages (like JavaScript e.g.) this works differently. So the compiler could theoretically translate your code, capture the value, and continue, but it would have to store that value differently, and you would probably get unexpected results. Therefore the team developing lambdas for Java 8 correctly excluded this scenario, by preventing it with an exception.



            If you ever need to change values of external variables within lambda expressions, you can either declare a one-element array:



            String myStringRef = { "before" };
            someCallingMethod(() -> myStringRef[0] = "after" );
            System.out.println(myStringRef[0]);


            Or use Atomic to make it thread-safe. However with your example, this would probably return "before" since the thread would most likely execute after the execution of println.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago

























            answered 1 hour ago









            TreffnonXTreffnonX

            35219




            35219

























                2














                In an enhanced for loop the variable is initialized for each iteration. From §14.14.2 of the JLS:




                ...



                When an enhanced for statement is executed, the local variable is initialized, on each iteration of the loop, to successive elements of the array or Iterable produced by the expression. The precise meaning of the enhanced for statement is given by translation into a basic for statement, as follows:





                • If the type of Expression is a subtype of Iterable, then the translation is as follows.



                  If the type of Expression is a subtype of Iterable for some type argument X, then let I be the type java.util.Iterator; otherwise, let I be the raw type java.util.Iterator.



                  The enhanced for statement is equivalent to a basic for statement of the form:



                  for (I #i = Expression.iterator(); #i.hasNext(); ) {
                  {VariableModifier} TargetType Identifier =
                  (TargetType) #i.next();
                  Statement
                  }



                ...





                • Otherwise, the Expression necessarily has an array type, T.



                  Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement.



                  The enhanced for statement is equivalent to a basic for statement of the form:



                  T #a = Expression;
                  L1: L2: ... Lm:
                  for (int #i = 0; #i < #a.length; #i++) {
                  {VariableModifier} TargetType Identifier = #a[#i];
                  Statement
                  }



                ...




                In other words, your enhanced for loop is equivalent to:



                ArrayList<Integer> listOfInt = new ArrayList<>();
                // add elements...

                for (Iterator<Integer> itr = listOfInt.iterator(); itr.hasNext(); ) {
                Integer arg = itr.next();
                new Thread(() -> System.out.println(arg)).start();
                }


                Since the variable is initialized each iteration it is effectively final (unless you modify the variable inside the loop).



                In contrast, the variable in the basic for loop (k in your case) is initialized once and updated each iteration (if a "ForUpdate" is present, e.g. k++). See §14.14.1 of the JLS for more information. Since the variable is updated each iteration is is not final nor effectively final.






                share|improve this answer






























                  2














                  In an enhanced for loop the variable is initialized for each iteration. From §14.14.2 of the JLS:




                  ...



                  When an enhanced for statement is executed, the local variable is initialized, on each iteration of the loop, to successive elements of the array or Iterable produced by the expression. The precise meaning of the enhanced for statement is given by translation into a basic for statement, as follows:





                  • If the type of Expression is a subtype of Iterable, then the translation is as follows.



                    If the type of Expression is a subtype of Iterable for some type argument X, then let I be the type java.util.Iterator; otherwise, let I be the raw type java.util.Iterator.



                    The enhanced for statement is equivalent to a basic for statement of the form:



                    for (I #i = Expression.iterator(); #i.hasNext(); ) {
                    {VariableModifier} TargetType Identifier =
                    (TargetType) #i.next();
                    Statement
                    }



                  ...





                  • Otherwise, the Expression necessarily has an array type, T.



                    Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement.



                    The enhanced for statement is equivalent to a basic for statement of the form:



                    T #a = Expression;
                    L1: L2: ... Lm:
                    for (int #i = 0; #i < #a.length; #i++) {
                    {VariableModifier} TargetType Identifier = #a[#i];
                    Statement
                    }



                  ...




                  In other words, your enhanced for loop is equivalent to:



                  ArrayList<Integer> listOfInt = new ArrayList<>();
                  // add elements...

                  for (Iterator<Integer> itr = listOfInt.iterator(); itr.hasNext(); ) {
                  Integer arg = itr.next();
                  new Thread(() -> System.out.println(arg)).start();
                  }


                  Since the variable is initialized each iteration it is effectively final (unless you modify the variable inside the loop).



                  In contrast, the variable in the basic for loop (k in your case) is initialized once and updated each iteration (if a "ForUpdate" is present, e.g. k++). See §14.14.1 of the JLS for more information. Since the variable is updated each iteration is is not final nor effectively final.






                  share|improve this answer




























                    2












                    2








                    2







                    In an enhanced for loop the variable is initialized for each iteration. From §14.14.2 of the JLS:




                    ...



                    When an enhanced for statement is executed, the local variable is initialized, on each iteration of the loop, to successive elements of the array or Iterable produced by the expression. The precise meaning of the enhanced for statement is given by translation into a basic for statement, as follows:





                    • If the type of Expression is a subtype of Iterable, then the translation is as follows.



                      If the type of Expression is a subtype of Iterable for some type argument X, then let I be the type java.util.Iterator; otherwise, let I be the raw type java.util.Iterator.



                      The enhanced for statement is equivalent to a basic for statement of the form:



                      for (I #i = Expression.iterator(); #i.hasNext(); ) {
                      {VariableModifier} TargetType Identifier =
                      (TargetType) #i.next();
                      Statement
                      }



                    ...





                    • Otherwise, the Expression necessarily has an array type, T.



                      Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement.



                      The enhanced for statement is equivalent to a basic for statement of the form:



                      T #a = Expression;
                      L1: L2: ... Lm:
                      for (int #i = 0; #i < #a.length; #i++) {
                      {VariableModifier} TargetType Identifier = #a[#i];
                      Statement
                      }



                    ...




                    In other words, your enhanced for loop is equivalent to:



                    ArrayList<Integer> listOfInt = new ArrayList<>();
                    // add elements...

                    for (Iterator<Integer> itr = listOfInt.iterator(); itr.hasNext(); ) {
                    Integer arg = itr.next();
                    new Thread(() -> System.out.println(arg)).start();
                    }


                    Since the variable is initialized each iteration it is effectively final (unless you modify the variable inside the loop).



                    In contrast, the variable in the basic for loop (k in your case) is initialized once and updated each iteration (if a "ForUpdate" is present, e.g. k++). See §14.14.1 of the JLS for more information. Since the variable is updated each iteration is is not final nor effectively final.






                    share|improve this answer















                    In an enhanced for loop the variable is initialized for each iteration. From §14.14.2 of the JLS:




                    ...



                    When an enhanced for statement is executed, the local variable is initialized, on each iteration of the loop, to successive elements of the array or Iterable produced by the expression. The precise meaning of the enhanced for statement is given by translation into a basic for statement, as follows:





                    • If the type of Expression is a subtype of Iterable, then the translation is as follows.



                      If the type of Expression is a subtype of Iterable for some type argument X, then let I be the type java.util.Iterator; otherwise, let I be the raw type java.util.Iterator.



                      The enhanced for statement is equivalent to a basic for statement of the form:



                      for (I #i = Expression.iterator(); #i.hasNext(); ) {
                      {VariableModifier} TargetType Identifier =
                      (TargetType) #i.next();
                      Statement
                      }



                    ...





                    • Otherwise, the Expression necessarily has an array type, T.



                      Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement.



                      The enhanced for statement is equivalent to a basic for statement of the form:



                      T #a = Expression;
                      L1: L2: ... Lm:
                      for (int #i = 0; #i < #a.length; #i++) {
                      {VariableModifier} TargetType Identifier = #a[#i];
                      Statement
                      }



                    ...




                    In other words, your enhanced for loop is equivalent to:



                    ArrayList<Integer> listOfInt = new ArrayList<>();
                    // add elements...

                    for (Iterator<Integer> itr = listOfInt.iterator(); itr.hasNext(); ) {
                    Integer arg = itr.next();
                    new Thread(() -> System.out.println(arg)).start();
                    }


                    Since the variable is initialized each iteration it is effectively final (unless you modify the variable inside the loop).



                    In contrast, the variable in the basic for loop (k in your case) is initialized once and updated each iteration (if a "ForUpdate" is present, e.g. k++). See §14.14.1 of the JLS for more information. Since the variable is updated each iteration is is not final nor effectively final.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 1 hour ago

























                    answered 1 hour ago









                    SlawSlaw

                    7,6283932




                    7,6283932























                        0














                        An enhanced for loop is defined to be equivalent to this code:



                        for (Iterator<T> it = iterable.iterator(); it.hasNext(); ) {
                        T loopvar = it.next();

                        }


                        This substitution code explains why the variable of an enhanced for loop is considered effectively final.






                        share|improve this answer




























                          0














                          An enhanced for loop is defined to be equivalent to this code:



                          for (Iterator<T> it = iterable.iterator(); it.hasNext(); ) {
                          T loopvar = it.next();

                          }


                          This substitution code explains why the variable of an enhanced for loop is considered effectively final.






                          share|improve this answer


























                            0












                            0








                            0







                            An enhanced for loop is defined to be equivalent to this code:



                            for (Iterator<T> it = iterable.iterator(); it.hasNext(); ) {
                            T loopvar = it.next();

                            }


                            This substitution code explains why the variable of an enhanced for loop is considered effectively final.






                            share|improve this answer













                            An enhanced for loop is defined to be equivalent to this code:



                            for (Iterator<T> it = iterable.iterator(); it.hasNext(); ) {
                            T loopvar = it.next();

                            }


                            This substitution code explains why the variable of an enhanced for loop is considered effectively final.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 1 hour ago









                            Roland IlligRoland Illig

                            29.5k96091




                            29.5k96091






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54340101%2fenhanced-for-loop-and-lambda-expressions%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Liste der Baudenkmale in Friedland (Mecklenburg)

                                Single-Malt-Whisky

                                Czorneboh