Is a class being instantiated in a lambda expression? [duplicate]

你离开我真会死。 提交于 2019-12-01 05:37:09

The value of a lambda expression is a reference to an instance of a class. So, in practical terms, yes, an instance of a class is being created. See what the docs say:

At run time, evaluation of a lambda expression is similar to evaluation of a class instance creation expression, insofar as normal completion produces a reference to an object.

However, there is more to that than we can "see". There are many optimizations running under the hood. Depending on certain factors, a previously created object can, for example, be used again. This means that a new object need not be allocated on every evaluation of a lambda expression. Let's take a look at the docs:

Evaluation of a lambda expression is distinct from execution of the lambda body. 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.

[...]

These rules are meant to offer flexibility to implementations of the Java programming language, in that:

  • A new object need not be allocated on every evaluation.

  • Objects produced by different lambda expressions need not belong to different classes (if the bodies are identical, for example).

  • Every object produced by evaluation need not belong to the same class (captured local variables might be inlined, for example).

  • If an "existing instance" is available, it need not have been created at a previous lambda evaluation (it might have been allocated during the enclosing class's initialization, for example).

As you might have noticed, this is a complex topic. For a deeper understanding, take a look at the The Java® Language Specification, chapter “15.27.4. Run-time Evaluation of Lambda Expressions”.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!