Captured value of a lambda semantics

Remi Forax forax at univ-mlv.fr
Thu Apr 23 13:56:45 UTC 2015


Hi guys,
I've shown to some of my students how to play with the REPL
(and how to compile it in Eclipse but that's another war story ...)

One of my student send me this snippet:
    9 : import java.util.function.*;
   10 : IntUnaryOperator addOne = x -> x + 1;
   11 : addOne.applyAsInt(2)
   12 : addOne.applyAsInt(2
        )
   13 : addOne.applyAsInt(3)
   14 : IntUnaryOperator addTwo = x -> x + a;
   15 : int a = 2;
   16 : addTwo.applyAsInt(2)
   17 : a = 3
   18 : addTwo.applyAsInt(2)

as you may guess, the last expression result is 5 which is not the 
standard Java semantics,
is there a reason for that, or is this a side effect of declaring 'a' 
after the lambda ?

cheers,
Rémi



More information about the kulla-dev mailing list