Are compiled Java 8 lambda expressions backwards compatible with earlier versions of the Java runtime?

别来无恙 提交于 2019-12-18 03:58:27

问题


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<T> {
    public void perform(T argument);
}

The following two snippets are "functionally" equivalent:

With lambda:

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

With anonymous class instance:

final Action<String> y = new Action<String>() {
    @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?


回答1:


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.




回答2:


Officially no, but for an unofficial solution you should look at the Retrolambda project. It doesn't backport the Collection API changes, but it can handle lambda expressions (and method references) for you.




回答3:


I dont believe so - the bytecode version is different (52 i think) and lambda uses invokedynamic and not get translated into anonymous classes..




回答4:


Java 8 introduces a new concept of default method implementation in interfaces. This capability is added for backward compatibility so that old interfaces can be used to leverage the lambda expression capability of Java 8. For example, ‘List’ or ‘Collection’ interfaces do not have ‘forEach’ method declaration. Thus, adding such method will simply break the collection framework implementations. Java 8 introduces default method so that List/Collection interface can have a default implementation of forEach method, and the class implementing these interfaces need not implement the same.

public interface vehicle {
  default void print(){
      System.out.println("I am a vehicle!");
                       }
                          }


来源:https://stackoverflow.com/questions/22572230/are-compiled-java-8-lambda-expressions-backwards-compatible-with-earlier-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!