Easy Tutorial
❮ Java Encapsulation Data Vec Max ❯

Java 8 New Features

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


New Features

Java 8 introduces many new features, and we will mainly discuss the following:

-

Lambda Expressions - Lambda allows functions to be used as parameters for methods (passing functions into methods).

-

Method References - Method references provide a useful syntax that allows direct references to existing Java class or object (instance) methods or constructors. Combined with lambda, method references make the language constructs more compact and concise, reducing redundant code.

-

Default Methods - Default methods are methods with implementations within interfaces.

-

New Tools - New compiler tools, such as the Nashorn engine jjs, class dependency analyzer jdeps.

-

Stream API - The newly added Stream API (java.util.stream) brings true functional programming style to Java.

-

Date Time API - Enhanced handling of dates and times.

-

Optional Class - The Optional class has become part of the Java 8 library to address 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, refer to the official website: What's New in JDK 8

In the examples about Java 8, we use the jdk 1.8 environment. You can check the current jdk version with the following command:

$ 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 aims to have its own programming style, distinct from Java 7. The following example demonstrates 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("tutorialpro ");
      names1.add("Taobao ");
      names1.add("Baidu ");
      names1.add("Sina ");

      List<String> names2 = new ArrayList<String>();
      names2.add("Google ");
      names2.add("tutorialpro ");
      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);
   }

   // Sort using 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 using Java 8
private void sortUsingJava8(List<String> names) {
    Collections.sort(names, (s1, s2) -> s1.compareTo(s2));
}

Executing the above script outputs:

$ javac Java8Tester.java
$ java Java8Tester
Using Java 7 syntax:
[Baidu, Google, tutorialpro, Sina, Taobao]
Using Java 8 syntax:
[Baidu, Google, tutorialpro, Sina, Taobao]

Next, we will detail 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 and Time API
9 Base64

```

❮ Java Encapsulation Data Vec Max ❯