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

后端 未结 4 1249
囚心锁ツ
囚心锁ツ 2020-12-16 15:45

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 cons

相关标签:
4条回答
  • 2020-12-16 16:06

    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.

    0 讨论(0)
  • 2020-12-16 16:13

    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!");
                           }
                              }
    
    0 讨论(0)
  • 2020-12-16 16:15

    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.

    0 讨论(0)
  • 2020-12-16 16:23

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

    0 讨论(0)
提交回复
热议问题