编译的Java 8 lambda表达式是否向后兼容早期版本的Java运行时? [英] Are compiled Java 8 lambda expressions backwards compatible with earlier versions of the Java runtime?

查看:206
本文介绍了编译的Java 8 lambda表达式是否向后兼容早期版本的Java运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了减少由匿名类型的大量实例化引起的混乱,我正在探索利用Java 8 lambdas的可能性。

In order to reduce the clutter caused by numerous instantiations of anonymous types, I'm exploring the possibility of leveraging Java 8 lambdas.

在生产环境中使用Java 8和lambdas之前的一个重要考虑因素是,使用lambda表达式的JDK8编译代码是否可以在早期版本的Java运行时上执行。我特别感兴趣的是JRE6和JRE7作为目标平台。

One important consideration before using Java 8 and lambdas in a production environment is whether JDK8-compiled code that uses lambda expressions can be executed on an earlier version of the Java runtime. I'm specifically interested in JRE6 and JRE7 as target platforms.

一方面,我明白lambdas只是包含一个方法的匿名类实例化的语法糖。另一方面,我不确定这种等价意味着为每个生成的字节码在JRE8以外的JVM版本中是相同的和/或兼容的。

One one hand, I understand that lambdas are simply syntactic sugar around an instantiation of an anonymous class containing one method. On the other hand, I'm not certain that this equivalence implies that the bytecode generated for each is identical and/or compatible across JVM versions other than JRE8.

例如,给定单方法接口:

For example, given the single-method interface:

public interface Action<T> {
    public void perform(T argument);
}

以下两个片段在功能上是等效的:

The following two snippets are "functionally" equivalent:

使用lambda:

final Action<String> y = i -> System.out.println(i);

使用匿名类实例:

final Action<String> y = new Action<String>() {
    @Override
    public void perform(final String i) {
        System.out.println(i);
    }
};

我的具体问题是这两种结构的语义等价是否扩展到等价他们的编译表示。此外,如果它们确实等价地编译,那么这个等价是否表明lambda表达式的编译形式可以在没有修改的情况下托管在早期版本的Java运行库上?

My specific question is whether the semantic equivalence of both constructs extends to equivalence of their compiled representations. Furthermore, if they indeed compile equivalently, does this equivalence indicate that the compiled form of a lambda expression can be hosted on earlier versions of the Java runtime without modification?

推荐答案

通常,Javac编译器不可能使用高于目标JVM级别的源级别。因此答案是否定的。

In general it is not possible to have Javac compiler use a source level that is higher than the target JVM level. Thus the answer is NO.

这篇关于编译的Java 8 lambda表达式是否向后兼容早期版本的Java运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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