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:
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 |