JAVA8 new features

New features of Java8: 1) Default method of interface, Java 8 allows us to add a non-abstract method to an interface, just use the default keyword. 2) lambda expressions, before java8, if you want to pass behavior into functions, the only choice is anonymous classes, and the most important line of code that defines behavior is not prominent enough in the middle. Lambda expressions replace anonymous classes for cleaner coding. 3) Functional interface: refers to an interface with only one abstract method, and every lambda expression of this type will be matched to this abstract method. Each lambda expression corresponds to a type, usually an interface type. We can treat a lambda expression as any interface type that contains only one abstract method. In order to ensure that the interface must meet this requirement (that is, there is an abstract method), you only need to You need to annotate your interface with @FunctioalInterface

(The compiler will report an error if it finds that the interface marked with this annotation has more than one abstract method). 4) lambda scope, accessing the outer scope in the lambda expression is very similar to the method in the old version of the anonymous object, you can directly access the outer local variables or instance fields and static variables marked as final. Lambda expressions are only readable and non-writable to outer local variables, and readable and writable to class instance variables. 5) dateAPI: java8 includes a new set of date APIs in the java.time package. 6) annotation annotation, java8 supports repeatable annotation, the same annotation can be used multiple times in the same place.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324468483&siteId=291194637