Java 8 是否支持闭包? [英] Does Java 8 Support Closures?

查看:24
本文介绍了Java 8 是否支持闭包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑.我认为 Java 8 会从石器时代出现并开始支持 lambdas/closures.但是当我尝试这个时:

I'm confused. I thought Java 8 was going to emerge from the stone age and start supporting lambdas/closures. But when I try this:

public static void main(String[] args) {
    int number = 5;

    ObjectCallback callback = () -> {
        return (number = number + 1);
    };

    Object result = callback.Callback();
    System.out.println(result);
}

...它说数字应该是有效的最终.那是呃,我认为不是结束.这听起来像是按值复制环境,而不是按引用.

...it says that number should be effectively final. That's uh, not a closure I think. That just sounds like it's copying the environment by value, rather than by reference.

奖励问题!

Android 是否支持 Java 8 功能?

Will Android support Java 8 features?

推荐答案

为什么哦为什么,Java.为什么啊为什么.

Why oh why, Java. Why oh why.

您需要与相关的 Oracle Java 团队成员进行长时间的(私人)讨论才能获得真正的答案.(如果他们愿意和你谈谈......)

You would need to hold a long (private) discussion with the relevant Oracle Java team members for the true answer. (If they would be willing to talk to you ...)

但我怀疑这是向后兼容性和项目资源限制的组合.从实用的角度来看,当前的方法足够好"这一事实.

But I suspect it is a combination of backwards compatibility and project resourcing constraints. And the fact that the current approach is "good enough" from a pragmatic perspective.

将过程上下文实现为一等对象(即闭包)需要某些局部变量的生命周期超出声明方法调用的返回值.这意味着您不能将它们放在堆栈中.相反,您最终会遇到一些 局部变量必须是堆对象的字段的情况.这意味着您需要对 JVM 架构进行一种新的隐藏类或根本性更改.

Implementing procedure contexts as first-class objects (i.e. closures) requires that the lifetime of certain local variables extends beyond the return of the declaring method call. That means that you cannot just put them on the stack. Instead you end up with a situation where some local variables have to be fields of an heap object. That means you need a new kind of hidden class OR fundamental changes to the JVM architecture.

虽然在技术上可以实现这种事情,但 Java 语言并不是绿色领域"语言.在 Java 中支持真正的闭包"所需的性质的改变将是困难的:

While it is technically possible to implement this kind of thing, the Java language is not a "green field" language. A change of the nature that would needed to support "real closures" in Java would be difficult:

  • 更新所有工具链需要 Oracle 和第 3 方实施者付出大量努力.(我们不只是在谈论编译器.还有调试器、分析器、混淆器、字节码工程框架、持久性框架......)

  • It would take a huge amount of effort from Oracle and 3rd party implementors to update all of the tool chains. (And we are not just talking about compilers. There are debuggers, profilers, obfuscators, bytecode engineering frameworks, persistence frameworks ...)

然后,其中一些更改可能会影响数百万现有已部署 Java 应用程序的向后兼容性.

Then there is the risk that some of these changes would impact on backwards compatibility for the millions of existing deployed Java applications out there.

对以某种方式利用 JVM 的其他语言等有潜在影响.例如,Android 依赖 JVM 架构/字节码文件作为其 Davlik 工具链的输入语言".有 Python、Ruby 和各种函数式语言的语言实现,这些语言为 JVM 平台生成代码.

There is the potential impact on other languages, etc that leverage the JVM in some way. For example, Android depends on the JVM architecture / bytecode files as the "input language" for its Davlik tool-chain. There are language implementations for Python, Ruby and various functional languages that code generate for the JVM platform.

简而言之,Java 中的真正的闭包"对于每个相关的人来说都是一个可怕的命题.决赛结束"黑客是一种务实的妥协,确实有效,而且在实践中已经足够好了.

In short "real closures" in Java would be a big scary proposition for everyone concerned. The "closures for finals" hack is a pragmatic compromise that does work, and that is good enough in practice.

最后,final 限制总是有可能在未来的版本中被删除.(虽然我不会屏住呼吸......)

Lastly, there is always the possibility that the final restriction could be removed in a future edition. (I wouldn't hold my breath though ....)

android 是否支持 Java-8 特性?

Will android support Java-8 features?

除非有人拥有可靠的内部知识,否则这是不可能回答的.如果他们这样做了,他们会疯了在这里透露它.当然,Google 还没有宣布支持 Java 8.

That is impossible to answer unless someone has credible inside knowledge. And if they did, they would be crazy to reveal it here. Certainly Google have not announced support for Java 8.

但好消息是,KitKat 和相应版本的 Android Studio 或 Eclipse ADT 现在支持 Java 7 语法扩展.

这篇关于Java 8 是否支持闭包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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