English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java 8 (also known as jdk 1.8) is a major version of the Java language developed by Oracle Corporation in 2014 year 3 month 18 Java released in 8 Supports functional programming, new JavaScript engine, new date API, new Stream API, etc.
Java8 Many new features have been added, and we mainly discuss the following ones:
Lambda Expressions − Lambda allows functions to be passed as parameters to methods (functions are passed as parameters to methods).
Method References − Method references provide a very useful syntax that allows direct referencing of methods or constructors of existing Java classes or objects (examples). When used in conjunction with lambda expressions, method references can make the language's construction more compact and concise, reducing redundant code.
Default Methods − Default methods are methods that have 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) introduces a true functional programming style into Java.
Date Time API − Enhanced date and time processing.
Optional Class − Optional class has become Java 8 part of the class library, used to solve null pointer exceptions.
Nashorn, JavaScript Engine − Java 8Provided a new Nashorn JavaScript engine, which allows us to run specific JavaScript applications on the JVM.
More new features can be found on the official website:What's New in JDK 8
About Java 8 The examples in this article all use jdk 1.8 Environment, you can use the following command to view 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)
Java 8 Hope to have their own programming style, and distinguish from Java 7 differences, the following examples demonstrate Java 7 and Java 8 Programming format:
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("w3codebox "); names1.add("Taobao "); names1.add("Baidu "); names1.add("Sina "); List<String> names2 = new ArrayList<String>(); names2.add("Google "); names2.add("w3codebox "); 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); } // Using java 7 Sorting private void sortUsingJava7(List<String> names) { Collections.sort(names, new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.compareTo(s2); } ); } // Using java 8 Sorting 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 Using Java 7 Syntax: [Baidu, Google, w]3[Baidu, Google, w] Using Java 8 Syntax: [Baidu, Google, w]3[Baidu, Google, w]
Next, we will introduce Java in detail for everyone 8 New Features:
Serial Number | Features |
---|---|
1 | Lambda Expressions |
2 | Method References |
3 | Functional Interfaces |
4 | Default Methods |
5 | Stream |
6 | Optional Class |
7 | Nashorn, JavaScript Engine |
8 | New Date and Time API |
9 | Base64 |