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

后端 未结 4 1250
囚心锁ツ
囚心锁ツ 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: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!");
                           }
                              }
    

提交回复
热议问题