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, which supports functional programming, new JavaScript engine, new date API, new Stream API, etc.
New Features
Java8 has added a lot of new features. We mainly discuss the following ones:
Lambda expression Formula − Lambda allows the function to be used as a parameter of a method (the function is passed into the method as a parameter.
Method reference − Method reference provides a very useful syntax, you can directly reference methods or constructors of existing Java classes or objects (instances). Used in conjunction with lambda, method references can make the language structure more compact and concise, reducing redundant code.
#Default method- − The default method is a method that has an implementation in the interface
##New tool
− New. Compilation tools, such as: Nashorn engine jjs, class dependency analyzer jdeps ##Stream API
−The newly added Stream API (java.util.stream) converts the real The functional programming style is introduced into Java.##Date Time API − Enhances the processing of dates and times.
##Optional Class − The Optional class has become part of the Java 8 class library 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 articles about Java 8, we all use the 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)
Programming StyleJava 8 hopes to have its own programming style and distinguish it from Java 7. The following examples show the programming formats of Java 7 and Java 8:
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("php "); names1.add("Taobao "); names1.add("Baidu "); names1.add("Sina "); List<String> names2 = new ArrayList<String>(); names2.add("Google "); names2.add("php "); names2.add("Taobao "); names2.add("Baidu "); names2.add("Sina "); Java8Tester tester = new Java8Tester(); System.out.println("使用 Java 7 语法: "); tester.sortUsingJava7(names1); System.out.println(names1); System.out.println("使用 Java 8 语法: "); tester.sortUsingJava8(names2); System.out.println(names2); } // 使用 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); } }); } // 使用 java 8 排序 private void sortUsingJava8(List<String> names){ Collections.sort(names, (s1, s2) -> s1.compareTo(s2)); } }
$ javac Java8Tester.java $ java Java8Tester 使用 Java 7 语法: [Baidu , Google , php , Sina , Taobao ] 使用 Java 8 语法: [Baidu , Google , php , Sina , Taobao ]
Next we will introduce the new features of Java 8 in detail:
Serial numberFeatures
1
Lambda expression
Method reference | |
---|---|
Functional interface | |
Default method | |
Stream | |
Optional class | |
Nashorn, JavaScript engine | |
New date and time API | |
Base64 | |