Java 8 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; it supports functional programming, a new JavaScript engine, a new date API, a new Stream API, and more.
New Features
Java8 added a great many features; we mainly discuss the following:
-
Lambda Expressions − Lambda allows a function to be treated as a method parameter (functions passed as parameters into methods).
-
Method References − Method references provide a very useful syntax that can directly reference methods or constructors of existing Java classes or objects (instances). Used together with lambda, method references make language constructs more compact and concise, reducing redundant code.
-
Default Methods − A default method is a method that has an implementation inside an interface.
-
New Tools − New compilation tools, such as the Nashorn engine jjs and the class dependency analyzer jdeps.
-
Stream API − The newly added Stream API (java.util.stream) brings a truly functional programming style into Java.
-
Date Time API − Strengthens the 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 that allows us to run specific javascript applications on the JVM.
For more new features, refer to the official website:What's New in JDK 8
In the examples in this 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 its own programming style, distinct from Java 7; the following example shows 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
(
"
"
)
;
names1
.
add
(
"
Runoob
"
)
;
names1
.
add
(
"
Taobao
"
)
;
names1
.
add
(
"
Baidu
"
)
;
names1
.
add
(
"
Sina
"
)
;
List
<
String
names2
=
new
ArrayList
<
String
(
);
names2
.
add
(
"
"
)
;
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 with 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 with java 8
private
void
sortUsingJava8
(
List
<
String
names
)
{
Collections.
sort
(
names
,
(
s1
,
s2
)
->
s1
.
compareTo
(
s2
)
)
;
}
}
Executing the above script produces the following output:
$ javac Java8Tester.java
$ java Java8Tester
Using Java 7 syntax:
[Baidu , Google , Runoob , Sina , Taobao ]
Using Java 8 syntax:
[Baidu , Google , Runoob , Sina , Taobao ]
Next we will give a detailed introduction to the new features of Java 8:
| 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 |