Java 8支持关闭? [英] Does Java 8 Support Closures?

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

问题描述

我很困惑。我认为Java8将从石器时代出现,并开始支持lambdas / closures。但是当我尝试:

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

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

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

它表示 code>。这是呃,不是一个关闭我想。



红利问题!


$

解决方案


为什么是为什么, Java。为什么为什么为什么。


您需要与相关的Oracle Java团队成员进行长时间(如果他们愿意和你谈谈...)






但我怀疑这是向后兼容性的组合和项目资源约束。从实用的角度来看,当前的方法是足够好的。



将过程上下文实现为第一类对象(即闭包)需要一定的局部变量扩展超出了声明方法调用的返回。这意味着你不能把它们放在堆栈上。相反,你最终会遇到一些情况,其中一些局部变量必须是堆对象的字段。这意味着你需要一种新的隐藏类或对JVM架构的根本改变。



虽然在技术上可能实现这种事情,Java语言不是一种绿色领域语言。改变性质需要支持真正的关闭将是困难的:




  • 这将需要大量的努力Oracle和第三方实现者更新所有工具链。 (我们不只是谈论编译器,有调试器,分析器,混淆器,字节码工程框架,持久性框架...)



  • 这些更改会对其他部分的潜在影响产生影响。 语言等,以某种方式利用JVM。例如,Android依赖于JVM架构/字节码文件作为其Davlik工具链的输入语言。







简而言之,Java中的真正的闭包对于每个人来说都是一个很大的可怕的命题。



最后,总会有<$ c的可能性$ c> final 限制可以在将来的版本中删除。 (我不会阻止我的呼吸,虽然....)







Java-8功能?


除非有可信的内部知识,否则不可能回答。如果他们这样做,他们会疯狂地在这里揭示它。当然Google还没有宣布支持Java 8。



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


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

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.

Bonus question!

Will android support Java-8 features?

解决方案

Why oh why, Java. Why oh why.

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.

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.

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 need to support "real closures" would be difficult:

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

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

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


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.

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


Will android support Java-8 features?

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.

But the good news is that Java 7 syntax extensions are now supported with KitKat and corresponding versions of Android Studio or Eclipse ADT.

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

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