As of the current JDK 1.8 implementation, it builds an anonymous object to hold the lambda function and calls the function on such object. Is this anonymous object reused in each call, or is an object re-created each time?
-
As far as I remember lamdas are not compiled into objects. Instead they are translated into methods of class where they was defined.mkrakhin– mkrakhin2015-02-11 11:35:10 +00:00Commented Feb 11, 2015 at 11:35
-
@mkrakhin lambdafaq.org/are-lambda-expressions-objectsAlex Suo– Alex Suo2015-02-11 12:09:46 +00:00Commented Feb 11, 2015 at 12:09
-
I couldn't find a link, but I read an article where was told, that Oracle engineers decided to not implement lambdas as instances of anonymous classes. They told that this approach will affect performance because of additional work for ClassLoaders and additional footprint in meta-space.mkrakhin– mkrakhin2015-02-11 12:14:45 +00:00Commented Feb 11, 2015 at 12:14
-
@mkrakhin AFAIK there is still no byte-code level support to stand alone functions in Java; while compiling this into just the wrapping class raises the problem if you have several lambdas inside the same class. I agree that this would affect class loader performance but I would appreciate if more concrete evidence / detail is presented.Alex Suo– Alex Suo2015-02-11 12:22:26 +00:00Commented Feb 11, 2015 at 12:22
-
I haven't told that they are standalone, they belong to class where they was defined.mkrakhin– mkrakhin2015-02-11 12:56:17 +00:00Commented Feb 11, 2015 at 12:56
1 Answer
It may be re-used, it may not.
From JLS 15.27.4:
Either a new instance of a class with the properties below is allocated and initialized, or an existing instance of a class with the properties below is referenced.
You cannot depend on it to be one or the other. The compiler and/or runtime can choose the one that will give the best result. (This is one of the benefits of lambdas over anonymous classes -- because every time you use new, even on an anonymous class, it is guaranteed to be a new object, they are unable to optimize it by re-using it, even though 99% of the time you don't care if they are the same object or not.)
In the case where the lambda captures variables from the surrounding scope, it is generally not possible to re-use the object because the value of the captured variables is state stored in the lambda object, and each time the lambda is evaluated (even if it's the same lambda in source code), it may capture different values of the captured variables. Only if the compiler can somehow guarantee that two particular evaluations of the lambda must capture the exact same value of variables, can the compiler re-use the object.
In the case where the lambda does not capture any variables, then all instantiations of that lambda are identical in behavior. So in this case, a single object can be re-used for all evaluations of that lambda. I believe that the current implementation of Java does only allocate one copy for the duration of the program in this case. But this is just an optimization that is implementation-dependent.
7 Comments
this. this is effectively a final local variable in all instance methods, and is captured in lambdas just like other local variables, if instance variables or methods are used. this can change between different calls to a method, so it needs to be stored just like any other captured local variable. Static variables are not "captured".