Latest web development tutorials

Java 8 new features

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


New Features

Java8 added a lot of features, we focused on the following:

  • Lambda expressions - Lambda allows function (function as a parameter passed into the method as a parameter to a method.

  • Method Reference - reference method provides a very useful syntax, you can directly reference an existing Java class or object (instance) method or constructor.In combination with lambda, method reference configuration can be made more compact and concise language to reduce redundant code.

  • The default method - a default method is implemented with a method in which the interface.

  • New tools - new compiler tools, such as: Nashorn engine jjs, class dependent parser jdeps.

  • Stream API - added new Stream API (java.util.stream) the true functional programming style introduced to Java.

  • Date Time API - to strengthen the date and time of processing.

  • Optional class - Optional class has become part of Java 8 class libraries, used to solve a null pointer exception.

  • Nashorn, JavaScript engine - Java 8 provides a new Nashorn javascript engine, which allows us to run on the JVM specific javascript applications.

More new features can be found in the official website: the What apos New in the JDK. 8

Java 8 article about the examples we use the jdk 1.8 environment, you can use the following command to view the current version of the 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 want to have their own programming style and distinguished with Java 7, the following example shows the 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("w3big ");
      names1.add("Taobao ");
      names1.add("Baidu ");
      names1.add("Sina ");
		
      List<String> names2 = new ArrayList<String>();
      names2.add("Google ");
      names2.add("w3big ");
      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));
   }
}

Implementation of the above script, output is:

$ javac Java8Tester.java
$ java Java8Tester
使用 Java 7 语法: 
[Baidu , Google , w3big , Sina , Taobao ]
使用 Java 8 语法: 
[Baidu , Google , w3big , Sina , Taobao ]

Next, we will brief you on the details of the new features Java 8:

No. characteristic
1 Lambda expressions
2 Method references
3 Function Interface
4 The default method
5 Stream
6 Optional Class
7 Nashorn, JavaScript engine
8 The new date and time API
9 Base64