Java 8 new features

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

New features

Java8 has added a lot of features, we mainly discuss the following:
  • Lambda Expression − Lambda allows a function to be used as a parameter of a method (the function is passed as a parameter into the method.
  • Method References - Method references provide a very useful syntax for directly referencing methods or constructors of existing Java classes or objects (instances). Used in conjunction with lambda, method references can make the language more compact and concise, reducing redundant code.
  • Default Method − The default method is a method that has an implementation inside the 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 true functional programming styles into Java.
  • Date Time API − Strengthens the processing of dates and times.
  • Optional class - The Optional class has become part of the Java 8 class library to handle 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.
More new features can be found on the official website: What's New in JDK 8
In the case of Java 8 articles, we all use the jdk 1.8 environment. You can use the following command to view the current version of jdk:
$ 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 is distinguished 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("Runoob "); names1.add("Taobao "); names1.add("Baidu "); names1.add("Sina "); List<String> names2 = new ArrayList<String>(); names2.add("Google "); 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("Use Java 8 syntax: "); tester.sortUsingJava8(names2); System.out.println(names2); } // use java 7 Sort
private void sortUsingJava7(List<String> names){ Collections.sort(names, new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.compareTo(s2); } }); } // Sort using java 8 private void sortUsingJava8(List<String> names){ Collections.sort(names, (s1, s2) -> s1.compareTo(s2)); } }
Execute the above script, the output is:
$ javac Java8Tester.java
$ java Java8Tester
Using Java 7 syntax: 
[Baidu , Google , Runoob , Sina , Taobao ]
Use Java 8 syntax: 
[Baidu , Google , Runoob , Sina , Taobao ]
Next we will detail the new features of Java 8 for everyone:
No.characteristic
1Lambda expression
2Method reference
3Functional interface
4Default method
5Stream
6Optional class
7Nashorn, JavaScript Engine
8New datetime API
9Base64

Comments