New features in Java 8
Java 8 (also known as JDK 1.8) is a major version of Java language development. Oracle Corporation released Java 8 on March 18, 2014, which supports functional programming, new JavaScript engines, new date APIs, new stream APIs, and more.
New features
Java8 has added a lot of features, we mainly discuss the following:
The Lambda expression −lambda allows the function to be passed as a parameter of a method (function as an argument into the method).
method Reference − A method reference provides a very useful syntax to directly reference a method or constructor that already has a Java class or object (instance). Used in conjunction with lambda, method references can make language constructs more compact and concise, reducing redundant code.
default Method − The default method is a method that has an implementation in the interface.
new Tools − New compiler tools, such as: Nashorn engine JJs, class-dependent parser jdeps.
Stream API − The newly added stream API (Java.util.stream) introduces a true functional programming style into Java.
Date Time API − strengthens the processing of dates and times.
The Optional class −optional class has become part of the Java 8 class library To resolve null pointer exceptions.
Nashorn, JavaScript engine −java 8 provides a new Nashorn JavaScript engine that allows us to run specific JavaScript applications on the JVM.
More new features can be found on the website: What ' s new in JDK 8
In the case of Java 8 articles, we use the JDK 1.8 environment, and you can view the current JDK version using the following command:
$ java-"1.8.0_31"1.8.0_31-64-bit Server VM (build 25.31-b07, Mixed mode)
Programming style
Java 8 wants its own programming style and differs from Java 7, and the following examples show the programming formats for Java 7 and Java 8:
Java8tester.java File Code:
Importjava.util.Collections;Importjava.util.List;Importjava.util.ArrayList;ImportJava.util.Comparator; Public classJava8tester { Public Static voidMain (String args[]) {List<String> names1 =NewArraylist<string>(); Names1.add ("Google"); Names1.add ("Runoob"); Names1.add ("Taobao"); Names1.add ("Baidu"); Names1.add ("Sina"); List<String> Names2 =NewArraylist<string>(); Names2.add ("Google"); Names2.add ("Runoob"); Names2.add ("Taobao"); Names2.add ("Baidu"); Names2.add ("Sina"); Java8tester Tester=NewJava8tester (); System.out.println ("Using Java 7 Syntax:"); Tester.sortusingjava7 (NAMES1); System.out.println (NAMES1); System.out.println ("Using Java 8 Syntax:"); Tester.sortusingjava8 (Names2); System.out.println (Names2); } //sorting using Java 7 Private voidSortUsingJava7 (list<string>names) {Collections.sort (names,NewComparator<string>() {@Override Public intCompare (string s1, string s2) {returnS1.compareto (S2); } }); } //sorting using Java 8 Private voidSortUsingJava8 (list<string>names) {Collections.sort (names, (S1, S2)-S1.compareto (S2)); }}
Execute the above script and the output is:
78 syntax: [Baidu, Google, Runoob, Sina, Taobao]
Next we'll give you a detailed introduction to the new features of Java 8:
Serial Number |
features |
1 |
Lambda expression |
2 |
Method reference |
3 |
Function-Type interface |
4 |
Default method |
5 |
Stream |
6 |
Optional class |
7 |
Nashorn, JavaScript engine |
8 |
New Date-Time API |
9 |
Base64 |
New features in Java 8