更新时间:2023-11-10 20:22:40
您无法确定为a返回的对象的身份lambda表达式。它可以是新实例,也可以是预先存在的实例。
You can't be sure about the identity of the object returned for a lambda expression. It can be a new instance, or a pre-existing instance.
这在 JLS§ 15.27.4 :
在运行时,lambda表达式的计算类似于类实例创建表达式的计算,只要正常完成产生对对象的引用即可。 lambda表达式的求值与lambda体的执行不同。
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. Evaluation of a lambda expression is distinct from execution of the lambda body.
分配并初始化具有以下属性的类的新实例,或者现有的实例引用具有以下属性的类。如果要创建一个新实例,但没有足够的空间来分配对象,则抛出一个OutOfMemoryError会突然评估lambda表达式。
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. If a new instance is to be created, but there is insufficient space to allocate the object, evaluation of the lambda expression completes abruptly by throwing an OutOfMemoryError.