10

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?

7
  • As far as I remember lamdas are not compiled into objects. Instead they are translated into methods of class where they was defined. Commented Feb 11, 2015 at 11:35
  • @mkrakhin lambdafaq.org/are-lambda-expressions-objects Commented 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. Commented 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. Commented Feb 11, 2015 at 12:22
  • I haven't told that they are standalone, they belong to class where they was defined. Commented Feb 11, 2015 at 12:56

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

7 Comments

1. For surrounding scope variable capturing it's generally true for the variables in the method; but I think if its referring the class level variables it should be reusable. 2. Can you give some evidence and/or discuss common implementation and logic about the reusability e.g. Oracle JDK and OpenJDK. I know it can be reused, but it's good to know when and how the byte code compiler or JIT does it.
Also do you have documentation link saying this implementation is JVM specific, not a standard?
@AlexSuoL: 1. From a certain point of view, only local variables can be "captured". I don't know what you mean by "class level variables". Instance variables are always accessed through member lookup on some reference; if not specified it's implicitly 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".
OK I think it's just a terminology issue of "capture" then :-)
@newacct "In the case where the lambda captures variables..." do you also imply that not only a new object created, but a new class constructed? If not, what should happen for a new class to be generated for the same lambda?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.