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 its 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 AtomicReference<T>
to make it thread-safe. However with your example, this would probably return "before" since the callback would most likely be executed after the execution of println.