Enhanced 'for' loop and lambda expressions
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 run the same logic with enhanced for-loop
everything is working fine:
List<Integer> listOfInt = new Arrays.asList(1, 2, 3);
for (Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
// OK to capture 'arg'
}
Why is it working fine for an enhanced for
loop and not for a normal loop, although the enhanced for
loop is also somewhere inside incrementing the variable as done by a normal loop.**
java lambda
add a comment |
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 run the same logic with enhanced for-loop
everything is working fine:
List<Integer> listOfInt = new Arrays.asList(1, 2, 3);
for (Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
// OK to capture 'arg'
}
Why is it working fine for an enhanced for
loop and not for a normal loop, although the enhanced for
loop is also somewhere inside incrementing the variable as done by a normal loop.**
java lambda
add a comment |
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 run the same logic with enhanced for-loop
everything is working fine:
List<Integer> listOfInt = new Arrays.asList(1, 2, 3);
for (Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
// OK to capture 'arg'
}
Why is it working fine for an enhanced for
loop and not for a normal loop, although the enhanced for
loop is also somewhere inside incrementing the variable as done by a normal loop.**
java lambda
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 run the same logic with enhanced for-loop
everything is working fine:
List<Integer> listOfInt = new Arrays.asList(1, 2, 3);
for (Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
// OK to capture 'arg'
}
Why is it working fine for an enhanced for
loop and not for a normal loop, although the enhanced for
loop is also somewhere inside incrementing the variable as done by a normal loop.**
java lambda
java lambda
edited Jan 24 at 18:38


Peter Mortensen
13.8k1986113
13.8k1986113
asked Jan 24 at 5:48


Show StopperShow Stopper
5,4102068
5,4102068
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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 Atomic<T> 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.
add a comment |
In an enhanced for loop the variable is initialized every iteration. From §14.14.2 of the Java Language Specification (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 orIterable
produced by the expression. The precise meaning of the enhancedfor
statement is given by translation into a basicfor
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<X>
for some type argumentX
, then letI
be the typejava.util.Iterator<X>
; otherwise, letI
be the raw typejava.util.Iterator
.
The enhanced
for
statement is equivalent to a basicfor
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 enhancedfor
statement.
The enhanced
for
statement is equivalent to a basicfor
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.
The need for a final or effectively final variable is mandated and explained by §15.27.2 of the JLS:
...
Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared
final
or be effectively final (§4.12.4), or a compile-time error occurs where the use is attempted.
Any local variable used but not declared in a lambda body must be definitely assigned (§16 (Definite Assignment)) before the lambda body, or a compile-time error occurs.
Similar rules on variable use apply in the body of an inner class (§8.1.3). The restriction to effectively final variables prohibits access to dynamically-changing local variables, whose capture would likely introduce concurrency problems. Compared to the
final
restriction, it reduces the clerical burden on programmers.
The restriction to effectively final variables includes standard loop variables, but not enhanced-
for
loop variables, which are treated as distinct for each iteration of the loop (§14.14.2).
...
That last sentence even explicitly mentions the difference between basic for loop variables and enhanced for loop variables.
add a comment |
The other replies are helpful, but they don't seem to tackle the question directly and answer it in clear terms.
In your first example, you're trying to access k
from the lambda expression. The problem here is that k
changes its value over time (k++
is called after each loop iteration). Lambda expressions do capture external references, but they need to be marked as final
or be "effectively final" (i.e., marking them as final
would still produce valid code). This is to prevent concurrency problems; by the time the thread you created is run, k
could already hold a new value.
In your second example, on the other hand, the variable you're accessing is arg
, which is reinitialized with every iteration of the enhanced for-loop (compare with the example above, where k
was merely updated), so you're creating an entirely new variable with each iteration. As an aside, you can also explicitly declare the iteration variable of an enhanced for-loop as final
:
for (final Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
}
This ensures that the value arg
references won't have changed by the time the thread you created is run.
Nice clarification
– noamt
Jan 30 at 8:55
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 Atomic<T> 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.
add a comment |
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 Atomic<T> 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.
add a comment |
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 Atomic<T> 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.
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 Atomic<T> 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.
edited Jan 24 at 18:44


Peter Mortensen
13.8k1986113
13.8k1986113
answered Jan 24 at 5:56


TreffnonXTreffnonX
868611
868611
add a comment |
add a comment |
In an enhanced for loop the variable is initialized every iteration. From §14.14.2 of the Java Language Specification (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 orIterable
produced by the expression. The precise meaning of the enhancedfor
statement is given by translation into a basicfor
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<X>
for some type argumentX
, then letI
be the typejava.util.Iterator<X>
; otherwise, letI
be the raw typejava.util.Iterator
.
The enhanced
for
statement is equivalent to a basicfor
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 enhancedfor
statement.
The enhanced
for
statement is equivalent to a basicfor
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.
The need for a final or effectively final variable is mandated and explained by §15.27.2 of the JLS:
...
Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared
final
or be effectively final (§4.12.4), or a compile-time error occurs where the use is attempted.
Any local variable used but not declared in a lambda body must be definitely assigned (§16 (Definite Assignment)) before the lambda body, or a compile-time error occurs.
Similar rules on variable use apply in the body of an inner class (§8.1.3). The restriction to effectively final variables prohibits access to dynamically-changing local variables, whose capture would likely introduce concurrency problems. Compared to the
final
restriction, it reduces the clerical burden on programmers.
The restriction to effectively final variables includes standard loop variables, but not enhanced-
for
loop variables, which are treated as distinct for each iteration of the loop (§14.14.2).
...
That last sentence even explicitly mentions the difference between basic for loop variables and enhanced for loop variables.
add a comment |
In an enhanced for loop the variable is initialized every iteration. From §14.14.2 of the Java Language Specification (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 orIterable
produced by the expression. The precise meaning of the enhancedfor
statement is given by translation into a basicfor
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<X>
for some type argumentX
, then letI
be the typejava.util.Iterator<X>
; otherwise, letI
be the raw typejava.util.Iterator
.
The enhanced
for
statement is equivalent to a basicfor
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 enhancedfor
statement.
The enhanced
for
statement is equivalent to a basicfor
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.
The need for a final or effectively final variable is mandated and explained by §15.27.2 of the JLS:
...
Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared
final
or be effectively final (§4.12.4), or a compile-time error occurs where the use is attempted.
Any local variable used but not declared in a lambda body must be definitely assigned (§16 (Definite Assignment)) before the lambda body, or a compile-time error occurs.
Similar rules on variable use apply in the body of an inner class (§8.1.3). The restriction to effectively final variables prohibits access to dynamically-changing local variables, whose capture would likely introduce concurrency problems. Compared to the
final
restriction, it reduces the clerical burden on programmers.
The restriction to effectively final variables includes standard loop variables, but not enhanced-
for
loop variables, which are treated as distinct for each iteration of the loop (§14.14.2).
...
That last sentence even explicitly mentions the difference between basic for loop variables and enhanced for loop variables.
add a comment |
In an enhanced for loop the variable is initialized every iteration. From §14.14.2 of the Java Language Specification (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 orIterable
produced by the expression. The precise meaning of the enhancedfor
statement is given by translation into a basicfor
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<X>
for some type argumentX
, then letI
be the typejava.util.Iterator<X>
; otherwise, letI
be the raw typejava.util.Iterator
.
The enhanced
for
statement is equivalent to a basicfor
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 enhancedfor
statement.
The enhanced
for
statement is equivalent to a basicfor
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.
The need for a final or effectively final variable is mandated and explained by §15.27.2 of the JLS:
...
Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared
final
or be effectively final (§4.12.4), or a compile-time error occurs where the use is attempted.
Any local variable used but not declared in a lambda body must be definitely assigned (§16 (Definite Assignment)) before the lambda body, or a compile-time error occurs.
Similar rules on variable use apply in the body of an inner class (§8.1.3). The restriction to effectively final variables prohibits access to dynamically-changing local variables, whose capture would likely introduce concurrency problems. Compared to the
final
restriction, it reduces the clerical burden on programmers.
The restriction to effectively final variables includes standard loop variables, but not enhanced-
for
loop variables, which are treated as distinct for each iteration of the loop (§14.14.2).
...
That last sentence even explicitly mentions the difference between basic for loop variables and enhanced for loop variables.
In an enhanced for loop the variable is initialized every iteration. From §14.14.2 of the Java Language Specification (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 orIterable
produced by the expression. The precise meaning of the enhancedfor
statement is given by translation into a basicfor
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<X>
for some type argumentX
, then letI
be the typejava.util.Iterator<X>
; otherwise, letI
be the raw typejava.util.Iterator
.
The enhanced
for
statement is equivalent to a basicfor
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 enhancedfor
statement.
The enhanced
for
statement is equivalent to a basicfor
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.
The need for a final or effectively final variable is mandated and explained by §15.27.2 of the JLS:
...
Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared
final
or be effectively final (§4.12.4), or a compile-time error occurs where the use is attempted.
Any local variable used but not declared in a lambda body must be definitely assigned (§16 (Definite Assignment)) before the lambda body, or a compile-time error occurs.
Similar rules on variable use apply in the body of an inner class (§8.1.3). The restriction to effectively final variables prohibits access to dynamically-changing local variables, whose capture would likely introduce concurrency problems. Compared to the
final
restriction, it reduces the clerical burden on programmers.
The restriction to effectively final variables includes standard loop variables, but not enhanced-
for
loop variables, which are treated as distinct for each iteration of the loop (§14.14.2).
...
That last sentence even explicitly mentions the difference between basic for loop variables and enhanced for loop variables.
edited Jan 24 at 14:53
answered Jan 24 at 6:19
SlawSlaw
9,28131235
9,28131235
add a comment |
add a comment |
The other replies are helpful, but they don't seem to tackle the question directly and answer it in clear terms.
In your first example, you're trying to access k
from the lambda expression. The problem here is that k
changes its value over time (k++
is called after each loop iteration). Lambda expressions do capture external references, but they need to be marked as final
or be "effectively final" (i.e., marking them as final
would still produce valid code). This is to prevent concurrency problems; by the time the thread you created is run, k
could already hold a new value.
In your second example, on the other hand, the variable you're accessing is arg
, which is reinitialized with every iteration of the enhanced for-loop (compare with the example above, where k
was merely updated), so you're creating an entirely new variable with each iteration. As an aside, you can also explicitly declare the iteration variable of an enhanced for-loop as final
:
for (final Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
}
This ensures that the value arg
references won't have changed by the time the thread you created is run.
Nice clarification
– noamt
Jan 30 at 8:55
add a comment |
The other replies are helpful, but they don't seem to tackle the question directly and answer it in clear terms.
In your first example, you're trying to access k
from the lambda expression. The problem here is that k
changes its value over time (k++
is called after each loop iteration). Lambda expressions do capture external references, but they need to be marked as final
or be "effectively final" (i.e., marking them as final
would still produce valid code). This is to prevent concurrency problems; by the time the thread you created is run, k
could already hold a new value.
In your second example, on the other hand, the variable you're accessing is arg
, which is reinitialized with every iteration of the enhanced for-loop (compare with the example above, where k
was merely updated), so you're creating an entirely new variable with each iteration. As an aside, you can also explicitly declare the iteration variable of an enhanced for-loop as final
:
for (final Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
}
This ensures that the value arg
references won't have changed by the time the thread you created is run.
Nice clarification
– noamt
Jan 30 at 8:55
add a comment |
The other replies are helpful, but they don't seem to tackle the question directly and answer it in clear terms.
In your first example, you're trying to access k
from the lambda expression. The problem here is that k
changes its value over time (k++
is called after each loop iteration). Lambda expressions do capture external references, but they need to be marked as final
or be "effectively final" (i.e., marking them as final
would still produce valid code). This is to prevent concurrency problems; by the time the thread you created is run, k
could already hold a new value.
In your second example, on the other hand, the variable you're accessing is arg
, which is reinitialized with every iteration of the enhanced for-loop (compare with the example above, where k
was merely updated), so you're creating an entirely new variable with each iteration. As an aside, you can also explicitly declare the iteration variable of an enhanced for-loop as final
:
for (final Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
}
This ensures that the value arg
references won't have changed by the time the thread you created is run.
The other replies are helpful, but they don't seem to tackle the question directly and answer it in clear terms.
In your first example, you're trying to access k
from the lambda expression. The problem here is that k
changes its value over time (k++
is called after each loop iteration). Lambda expressions do capture external references, but they need to be marked as final
or be "effectively final" (i.e., marking them as final
would still produce valid code). This is to prevent concurrency problems; by the time the thread you created is run, k
could already hold a new value.
In your second example, on the other hand, the variable you're accessing is arg
, which is reinitialized with every iteration of the enhanced for-loop (compare with the example above, where k
was merely updated), so you're creating an entirely new variable with each iteration. As an aside, you can also explicitly declare the iteration variable of an enhanced for-loop as final
:
for (final Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
}
This ensures that the value arg
references won't have changed by the time the thread you created is run.
edited Jan 30 at 1:20
answered Jan 24 at 11:43


Gustavo SilvaGustavo Silva
5314
5314
Nice clarification
– noamt
Jan 30 at 8:55
add a comment |
Nice clarification
– noamt
Jan 30 at 8:55
Nice clarification
– noamt
Jan 30 at 8:55
Nice clarification
– noamt
Jan 30 at 8:55
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 24 at 6:24
Roland IlligRoland Illig
30.5k96492
30.5k96492
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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