是否在lambda表达式中实例化了一个类? [英] Is a class being instantiated in a lambda expression?

查看:376
本文介绍了是否在lambda表达式中实例化了一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法调用,其中我传递一个lambda表达式。是否在这里隐式实例化了一个类?

I have the following method invocation, in which I am passing a lambda expression. Is a class being instantiated implicitly here?

printStudents(
    roster,
    (Student s) -> s.getGender() == Student.Sex.MALE
        && s.getAge() >= 18
        && s.getAge() <= 25
);

方法签名:

printStudents(List<Student> roster, CheckStudent checkstudet)


interface CheckStudent {
    boolean test(Student s);
}

修改

有些人建议我重构代码,但同样的问题出现了。

Some of you suggested me to refactor the code, but the same question arises.

CheckStudent checkStudent = (Student s) -> s.getGender() == Student.Sex.MALE && s.getAge() >= 18 && s.getAge() <= 25;

是一个班级(我不是指班级学生)在作业的右侧实例化?

Is a class (I am not referring to class Student ) being instantiated on the right side of the assignment?

推荐答案

lambda表达式的值是对类的实例的引用。所以,实际上,是的,正在创建一个类的实例。看看文档说的是什么:

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:


在运行时,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.

然而,还有比我们看到更多的东西。引擎盖下有许多优化。根据某些因素,例如,可以再次使用先前创建的对象。这意味着无需在每次评估lambda表达式时分配新对象。让我们来看看文档:

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:


lambda表达式的评估与
lambda体的执行不同。具有以下属性的类的新实例是
已分配和初始化,或者引用下面具有
属性的类的现有实例。

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.

[...]

这些规则旨在为
Java编程语言的实现提供灵活性,其中包括:

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.

对象由不同的lambda表达式产生的不必属于不同的类(例如,如果主体是相同的)。

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).

如果现有实例可用,则无需在之前创建lambda评估(例如,在封闭类的初始化期间可能已经分配了
)。

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).

您可能已经注意到,这是一个复杂的主题。为了更深入地理解,请查看Java®语言规范,15.27.4。 Lambda表达式的运行时评估

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".

这篇关于是否在lambda表达式中实例化了一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆