Java 8 Lambda变量范围 [英] Java 8 Lambda variable scope
问题描述
我有2个代码示例:
int[] idx = { 0 };
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("def");
list.add("ghi");
list.stream().forEach(item -> {
System.out.println(idx[0] + ": " + item);
idx[0]++;
});
正常工作。
此代码编译错误:
int idx = 0;
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("def");
list.add("ghi");
list.stream().forEach(item -> {
System.out.println(idx + ": " + item);
idx++;
});
说:
Local variable idx defined in an enclosing scope must be final or effectively final.
唯一的区别是 idx
int或int数组。
The only difference is idx
int or int array.
根本原因是什么?
推荐答案
根本原因是JVM缺少构造对局部变量的引用的机制,当 idx
是一个时,需要执行 idx ++
int
或某些不可变类型(例如 String
)。由于您尝试改变 idx
,因此仅仅捕获其值是不够的; Java需要捕获一个引用,然后通过它修改它。
The root cause is that JVM lacks mechanisms of constructing references to local variables, which is what is needed to perform idx++
when idx
is an int
or some immutable type (e.g. String
). Since you try to mutate idx
, simply capturing its value would not be sufficient; Java would need to capture a reference, and then modify the value through it.
使用数组时Java没有这个问题,因为数组是引用对象。 Java可以捕获永不改变的数组引用,并使用该不变的引用来改变对象。数组本身提供了必要的间接级别,因为Java数组是可变的。
Java does not have this problem when you use an array, because arrays are reference objects. Java can capture array reference that never changes, and use that non-changing reference to mutate the object. Array itself provides the necessary level of indirection, because Java arrays are mutable.
我试过make
idx
static并将其移出main方法,工作正常。但为什么?
I tried make
idx
static and move it outside the main method, working fine. But why?
因为在这种情况下,lambda不需要捕获对基本类型的局部变量的引用。对静态变量的引用很容易获得,因此捕获它没有问题。
Because in this situation there is no need for the lambda to capture a reference to a local variable of primitive type. A reference to the static variable is readily available, so there is no problem with capturing it.
同样,如果你使 idx,代码也可以工作
一个成员变量,并在实例方法中使用lambda。这将允许lambda通过这个
对象修改 idx
字段,该对象可以自由捕获。
Similarly, the code would work if you make idx
a member variable, and use your lambda inside an instance method. This would let lambda modify idx
field through this
object, which could be freely captured.
这篇关于Java 8 Lambda变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!