Java8 New Features
Java 8 (also known as jdk 1.8) is a major version of Java language development. Oracle released Java 8 on March 18, 2014, which supports functional programming, a new JavaScript engine, new Date API, new Stream API, etc.
* * *
## New Features
Java 8 adds many new features, and we will mainly discuss the following:
* **Lambda Expressions** β Lambda allows you to pass a function as a parameter to a method (passing a function as an argument to a method).
* **Method References** β Method references provide very useful syntax that allows you to directly reference methods or constructors of existing Java classes or objects (instances). When used together with lambda, method references can make the language constructs more compact and concise, reducing redundant code.
* **Default Methods** β A default method is a method that has an implementation within an interface.
* **New Tools** β New compilation tools, such as: Nashorn engine jjs, class dependency analyzer jdeps.
* **Stream API** β The newly added Stream API (java.util.stream) brings true functional programming style to Java.
* **Date Time API** β Enhanced processing of dates and times.
* **Optional Class** β Optional class has become part of the Java 8 library to solve 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.
For more new features, please refer to the official website: [What's New in JDK 8](http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html)
In the examples of this article about Java 8, we all use jdk 1.8 environment. You can use the following command to check the current jdk version:
$ java -version java version "1.8.0_31"Java(TM) SE Runtime Environment (build 1.8.0_31-b13)Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
* * *
## Programming Style
Java 8 wants to have its own programming style and be distinguished from Java 7. The following example shows the programming format of 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[]){Listnames1 = new ArrayList(); names1.add("Google "); names1.add("Tutorial "); names1.add("Taobao "); names1.add("Baidu "); names1.add("Sina "); Listnames2 = new ArrayList(); names2.add("Google "); names2.add("Tutorial "); 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); }private void sortUsingJava7(Listnames){Collections.sort(names, new Comparator(){ @Override public int compare(String s1, String s2){return s1.compareTo(s2); }}); }private void sortUsingJava8(Listnames){Collections.sort(names, (s1, s2) ->s1.compareTo(s2)); }}
Execute the above script, the output is:
$ javac Java8Tester.java $ java Java8TesterUsing Java 7 Syntax: [Baidu , Google , Tutorial , Sina , Taobao ]Using Java 8 Syntax: [Baidu , Google , Tutorial , Sina , Taobao ]
Next, we will introduce the new features of Java 8 in detail:
| No. | Feature |
| --- | --- |
| 1 | (#) |
| 2 | (#) |
| 3 | (#) |
| 4 | (#) |
| 5 | (#) |
| 6 | (#) |
| 7 | [Nashorn, JavaScript Engine](#) |
| 8 | (http://
YouTip