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:
Lambda expression ? Lambda allows the function to be passed as a parameter of a method (function as a parameter into the method).
method Reference ? A method reference provides a very useful syntax for directly referencing 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 tool ? 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 ? Strengthen the processing of dates and times.
Optional class ? The 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-Versionjava version"1.8.0_31"Java(Tm)SE Runtime Environment (build 1.8. 0_31-b13)Java HotSpot(TM) -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:Import Java.Util.Collections;Import Java.Util.List;Import Java.Util.ArrayList;Import Java.Util.Comparator;Public Class Java8tester { Public Static void Main(String Args[]){ List<String>Names1=New ArrayList<String>();Names1.Add("Google");Names1.Add("Runoob");Names1.Add("Taobao");Names1.Add("Baidu");Names1.Add("Sina");List<String>Names2=New ArrayList<String>();Names2.Add("Google");Names2.Add("Runoob");Names2.Add("Taobao");Names2.Add("Baidu");Names2.Add("Sina");Java8tester Tester=New Java8tester();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 void SortUsingJava7(List<String>Names){ Collections.Sort(Names,New Comparator<String>() {@Override Public Int Compare(String S1,String S2) { Return S1.CompareTo(S2);} });} //Sorting using Java 8 Private void SortUsingJava8(List<String> names) { collectionssort (names (s1s2) s1< Span class= "Hl-code". compareto (s2 ) } } /span>
Execute the above script and the output is:
$ javacJava8tester.java$ JavaJava8testerUse Java 7 Grammar: [Baidu , Google , Runoob , sina , Taobao ] use java 8 syntax: [baidu ,google , runoob , sina ,< Span class= "PLN" > 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