Java - changing the value of a final variable from within a lambda
问题 In Java I have the following code List<Integer> myList = new ArrayList<>(); for (int i=0;i<9;i++) { myList.add(i); } Integer sum = 0; myList.forEach(i -> { sum = sum + i; // does not compile, sum needs to be final or effectively final }); for(int i : myList) { sum = sum + i; //runs without problems } My question is, why is it exactly that I cannot change the value of sum from within the lambda? It does the exact same thing as the for loop down below, or am I wrong? Interesting is also the