Java 8 of the eight new features Chszs, not allowed to reprint without the Bo master. Permitted reprint must indicate author and blog homepage: http://blog.csdn.net/chszs1, interface default method and static method
Java 8 introduces new features in terms of interfaces.
Prior 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. This method uses the default keyword-decorated method name. In fact, the interface does not contain any implementation methods, whereas 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"); }}
2. Lambda expression
The most exciting feature in Java 8 is the lambda expression, which is able to pass functions/actions as arguments to the method.
package demo.ch;import java.util.Arrays;public class JavalamdaExpression { public static void main(String[] args) { Arrays.asList("j", "a", "v", "a", "8").forEach(e->System.out.print(e)); }}
3, 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 only 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.
package demo.ch;import java.util.Optional;public class Java8OptionalDemo { public static void main(String[] args) { Optional<String> str = Optional.ofNullable(null); System.out.println("str having value ? " + str.isPresent()); // output : str having value ? false }}
4, Streams
The stream API is the proof of the introduction of functional programming in Java 8, where the stream API provides functional manipulation of element flows, including list, set, map, and so on, as well as filtering filtering, mapping mapping, removing duplicate elements from the collection, and so on.
Stream stream can be obtained from collections, arrays, read buffers, and so on.
package demo.ch;import java.util.Arrays;public class Java8StreamsDemo { public static void main(String[] args) { Arrays.stream(new int[]{1, 2, 3, 4, 5}) .map(n->2*n+1) .average() .ifPresent(System.out::println); // output: 7.0 }}
5. Method reference
We can use lambda expressions to create an anonymous method. However, a lambda expression does not just call an existing method. In some cases, it is also used to explicitly specify an existing method by using the method name. By using the method reference by name, the code appears more compact and easy to read.
package demo.ch;import java.util.Arrays;public class Java8MethodRef { public static void main(String[] args) { Arrays.asList("a", "b", "c").forEach(new Java8MethodRef()::show); } public void show(String str){ System.out.print(str + " "); } // output: a b c}
6. 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
package demo.ch;import java.time.LocalDate;import java.time.Month;public class Java8DateTimeAPI { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); System.out.println(currentDate); // output: 2015-11-24 LocalDate twentyDecember2015 = LocalDate.of(2015, Month.DECEMBER, 20); System.out.println(twentyDecember2015); // output: 2015-12-20 LocalDate firstDec2015 = LocalDate.of(2015, 12, 1); System.out.println(firstDec2015); // output: 2015-12-01 }}
7. Nashorn JavaScript engine
Java 8 introduces a new Nashorn JavaScript engine that allows you to develop and run JavaScript applications.
package demo.ch;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import javax.script.ScriptException;public class Java8JavaScript { public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); System.out.println(engine.getClass().getName()); try { System.out.println("output: " + engine.eval("function show() {return 10;}; show();")); } catch (ScriptException e) { e.printStackTrace(); } // jdk.nashorn.api.scripting.NashornScriptEngine // output: 10 }}
8. Parallel array Sorting
In Java 7, the Arrays.sort () method has been used to sort objects, and in Java 8, a new parallel sort has been introduced, which is faster than the former and follows the Fork/join framework introduced by Java 7. You can assign a sort task to multiple threads that are 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.
package demo.ch;import java.util.Arrays;public class ParallelSort { public static void main(String[] args) { int arr[] = {1, 4, 2, 8, 5}; Arrays.parallelSort(arr); for(int i : arr){ System.out.print(i + " "); } } // output: 1 2 4 5 8 }
Eight new features of Java 8