JAVA8向下兼容JAVA6,编译的Java 8 lambda表达式是否向后兼容早期版本的Java运行时?...

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

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.

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 {

public void perform(T argument);

}

The following two snippets are "functionally" equivalent:

With lambda:

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

With anonymous class instance:

final Action y = new Action() {

@Override

public void perform(final String i) {

System.out.println(i);

}

};

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?

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值