Enhanced for loop and lambda expressions

  • A+

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.


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.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: