Java 8 New Features

Java 8 (also known as jdk 1.8) is a major release in Java language development. Oracle Corporation released Java 8 on March 18, 2014. It supports functional programming, a new JavaScript engine, a new date API, a new Stream API, and more.


New Features

Java 8 added a great many features; we mainly discuss the following ones:

  • Lambda Expressions− Lambda allows functions to be used as method parameters (functions passed into methods as arguments).

  • Method References− Method references provide very useful syntax that can directly reference methods or constructors of existing Java classes or objects (instances). When used together with lambda, method references can make language constructs more compact and concise, reducing redundant code.

  • Default Methods− A default method is a method that has an implementation in 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) introduces a truly functional programming style to Java.

  • Date Time API− Enhanced handling of dates and times.

  • Optional Class− The Optional class has become part of the Java 8 class library, used to solve null pointer exceptions.

  • Nashorn, JavaScript Engine− Java 8 provides a new Nashorn JavaScript engine, which 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

In the examples of the Java 8 article, we all use the 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 distinguish itself from Java 7. The following examples show the programming formats 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[]){ List<String> names1 = new ArrayList<String>(); names1.add("Google "); names1.add("Example "); names1.add("Taobao "); names1.add("Baidu "); names1.add("Sina "); List<String> names2 = new ArrayList<String>(); names2.add("Google "); names2.add("Example "); 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){ Collections.sort(names, (s1, s2) -> s1.compareTo(s2)); } }

Execute the above script, the output result is:

$ javac Java8Tester.java
$ java Java8Tester
使用 Java 7 语法: 
[Baidu , Google , Example , Sina , Taobao ]
使用 Java 8 语法: 
[Baidu , Google , Example , Sina , Taobao ]

Next, we will introduce the new features of Java 8 in detail for everyone:

No. Feature
1 Lambda Expressions
2 Method References
3 Functional Interfaces
4 Default Methods
5 Stream
6 Optional Class
7 Nashorn, JavaScript Engine
8 New Date/Time API
9 Base64
Other Extensions