Eight new features of Java 8
Eight new features of Java 8 1. Default and static methods of interfaces
Java 8 introduces new features in terms of interfaces.
Before Java version 8, interfaces only had abstract methods. In Java 8, two types of methods were added for interfaces.
The first method is the default method. This method uses the default keyword to modify the method name. In fact, the interface does not contain any implementation methods. In Java 8, you can use the default keyword to add the default method implementation.
The interface implementation class can directly use these default methods, and can also rewrite the default method, which is not mandatory rewrite.
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 the static method.
This is similar to the static method in the class. You can use the static keyword in the interface to define the static method. To call static methods defined by an interface, you only need to use the interface name to access these static methods. For example:
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 expressions
The most exciting feature of Java 8 is Lambda expressions, which can pass functions/actions as parameters to methods.
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 adds the Optional class to the java. util package. The Optional class is a container object that can contain or cannot contain non-null values. For each Java project, the primary duplicate statement is to check for NULL pointer exceptions NullPointerException. If any object is used, check whether the object is empty. if the object is not empty, execute the processing statement.
The Optional class is like a container that stores a value of the type or a 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
str = Optional.ofNullable(null); System.out.println("str having value ? " + str.isPresent()); // output : str having value ? false }}
4. Streams
Stream API proves that function programming is introduced in Java 8. Stream API provides function operations for element streams, including list, set, and map, it also supports filtering, ing, and removing repeated elements from the set.
You can obtain Stream streams from collections, arrays, and read buffers.
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 anonymous methods. However, Lambda expressions do not just call existing methods. In some cases, it is also checked to explicitly use the method name to specify the existing method. Using Method references by name makes the code 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 and Time API
Java 8 adopts the JSR 310 specification and adds the java. time package.
Before Java version 8, if we want to format a date, we must use the SimpleDateFormat class to format the input date class. Java 8 introduces the following new date and 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 can be used 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
Arrays already exists in Java 7. the sort () method can sort objects. in Java 8, a new parallel sorting method is introduced, which is faster than the former method, following the Fork/Join framework introduced by Java 7, you can allocate sorting tasks to multiple threads available in the thread pool.
Java 8 has added the parallel sorting function in the java. util. Arrays class to make full use of the multithreading mechanism.
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 }