The main new features of Java 8 include the following points:
the default method and static method of the interfacePrior to Java version 8, interfaces had only abstract methods, whereas in Java 8, there were two new types of methods for interfaces.
The first is the default method: in Java 8, you can add a default method implementation by using the Defaults keyword. An implementation class for an interface can use these default methods directly, and it can override the default method, which is not a mandatory override.
Package demo.ch; Public interface Java8interfacedemo {abstract void Add (); default void Display () {System.out.println ("default method of Interface"); }}
the second method introduced by the Java 8 interface is a static method . This is similar to static methods in a class, where static methods can be defined using the Static keyword in an interface. If we want to invoke the static method of the interface definition, we can access these static methods by simply using the interface name. Like what:
Package demo.ch; Public interface Java8interfacedemo {abstract void Add (); default void Display () {System.out.println ("default method of Interface"); } public static void Show () {System.out.println ("static method of Interface"); }}
second, lambda expression
lambda expression is not a stranger, his previous existence is an anonymous functionLambda expressions can be defined (imprecise, self-explanatory): a block of executable statements with input parameters Lambda syntax explained:
As can be seen from the general syntax of the lambda expression, it is quite consistent with the definition of the imprecise version given above – "A block of executable statements with input parameters".
The lambda expression syntax above can be thought of as the most complete version, which is slightly cumbersome to write. Don't worry, the following is a brief introduction to the various simplified versions of lambda expressions.
1. parameter type omitted – in most cases, the compiler can infer the parameter type of a lambda expression from the context. So the lambda expression becomes:
2. When the lambda expression has only one parameter number, you can omit the parentheses. The lambda expression is abbreviated as:
3. When a lambda expression contains only one statement, you can omit the curly braces, return, and the semicolon at the end of the statement. The lambda expression is simplified to:
Third, Java.util.Optional class
Java 8 has a new optional class in the Java.util package, and the optional class is a container object that can or cannot contain non-null values.
For each Java project, the main repeating statement is to check for null pointer exception nullpointerexception. We use any object, we need to check whether this object is empty, we can execute the processing statement if the object is not empty. The optional class is like a container that holds a value of type or null value. By using the Ispresent () method of the optional class, we can check whether the specified object is empty
Iv. Date Time API
Java 8 uses the JSR 310 specification and adds a new Java.time package. Before Java version 8, if we wanted to format the date, we had to use the SimpleDateFormat class to format the entered date class. Java 8 introduces the following new date-time classes:
Localtime,localdate, LocalDateTime, Offsetdate, Offsettime, Offsetdatetime
five, parallel array sorting
In Java 7, the Arrays.sort () method has been used to sort objects.
In Java 8, a new parallel ordering is introduced, which is faster than the former, and follows the Fork/join framework introduced by Java 7, which allows sorting tasks to be assigned to multiple threads available in the thread pool. Java 8 Adds a parallel sort feature to the Java.util.Arrays class to make more use of multithreaded mechanisms.
The new features introduced in each version of the JDK can be found in: http://www.cnblogs.com/langtianya/p/3757993.html
New features of Java8