TABLE OF CONTENTS (HIDE)

JDK 10 New Features

Introduction

In 2017, Oracle and the Java community announced its intentions to shift to a new six-month release cycle. JDK 10, relased in March 2018, is the first time-bound release. JDK 10 is more than a simple stability and performance fix over Java 9, it introduced twelve new enhancements defined through the Java Enhancement Proposals (JEPs), and some changes to the Java Standard Class Libraries.

var: Type Inference for Local-Variables with Initializers (JEP 286)

References:
  1. JEP 286: Local-Variable Type Inference @ http://openjdk.java.net/jeps/286.

The most notable change in the Java language specification in JDK 10 is the introduction of "var" to extend type inference for declarations of local variables with initializers, while maintaining Java's commitment to static type safety.

For example,

public class TestVar {
   public static void main(String[] anything) {
      // Primitives
      var i = 123;  // "int" inferred
      System.out.println(i);
      System.out.println(((Object)i).getClass().getName());
         // autobox to Integer, upcast to Object
         // java.lang.Integer

      var d = 12.34;  // "double" inferred
      System.out.println(d);
      System.out.println(((Object)d).getClass().getName());
         // autobox to Double, upcast to Object
         // java.lang.Double

      // Objects
      var str = "hello";  // "String" inferred
      System.out.println(str);
      System.out.println(str.getClass().getName());  // java.lang.String
      System.out.println(str instanceof String); // true


      var lst = new java.util.ArrayList<String>();  // "ArrayList" inferred
      System.out.println(lst);
      System.out.println(lst.getClass().getName());  // java.util.ArrayList
      System.out.println(lst instanceof java.util.ArrayList);  // true
      System.out.println(lst instanceof java.util.Collection); // true
   }
}

This feature is introduced in response to developers frequent complain about the degree of boilerplate codes required in Java. Type declarations for locals are often perceived to be unnecessary or even in the way; given good variable naming, it is often perfectly clear what is going on.

"var" is applicable to local variables with initializers, indexes in the enhanced for-loop, and locals declared in a traditional for-loop, for example,

public class TestVarLoop {
   public static void main(String[] anything) {

      // Traditional for-loop's int index
      for (var i = 0; i < 5; ++i) {   // "int" inferred
         System.out.println(i);
         System.out.println(((Object)i).getClass().getName());  // java.lang.Integer
      }

      // for-each loop
      var lst = new java.util.ArrayList<String>();  // "ArrayList" inferred
      lst.add("apple");
      lst.add("orange");
      lst.add("pear");
      for (var item : lst) {  // "String" inferred
         System.out.println(item);
         System.out.println(item.getClass().getName()); // java.lang.String
      }
   }
}

"var' is NOT applicable for method formals, constructor formals, method return types, fields, catch formals, or any other kind of variable declaration, for example,

var v1;
// error: cannot infer type for local variable v1
// (cannot use 'var' on variable without initializer)

var v2 = null;
// error: cannot infer type for local variable v2
// (variable initializer is 'null')

var v3 = {1, 2, 3};
// error: cannot infer type for local variable v3
// (array initializer needs an explicit target-type)

Java's var is similar to C++'s auto, but unlike JavaScript's var (because JavaScript is NOT static-type).

var is NOT a keyword, but a reserved type.

Other JEPs

Consolidate the JDK Forest into a Single Repository (JEP 296)

Combine the numerous repositories of the JDK forest into a single repository in order to simplify and streamline development.

Garbage-Collector Interface (JEP 304)

Improve the source code isolation of different garbage collectors by introducing a clean garbage collector (GC) interface.

Parallel Full GC for G1 (JEP 307)

Improve G1 worst-case latencies by making the full GC parallel.

Application Class-Data Sharing (JEP 310)

Class-Data Sharing (CDS) was introduced in Java 5 to improve the performance of the JVM startup and reduce the resource footprint when multiple JVMs are running on the same physical or virtual machine.

JDK 10 will extend CDS to allow the built-in system class loader, the built-in platform class loader, and custom class loaders to load archived classes. Previously, use of CDS had been restricted to the bootstrap class loader only.

Thread-Local Handshakes (JEP 312)

Introduce a way to execute a callback on threads without performing a global VM safepoint. Make it both possible and cheap to stop individual threads and not just all threads or none.

Remove the Native-Header Generation Tool (javah) (JEP 313)

Remove the javah tool from the JDK.

The javah tool was used to generate C/C++ header files for JNI (Java Native Interface) prior to Java 8. It is replaced by "javac -h".

Additional Unicode Language-Tag Extensions (JEP 314)

Enhance java.util.Locale and related APIs to implement additional Unicode extensions of BCP 47 language tags.

The following additional extensions are supported:

  • cu (currency type)
  • fw (first day of week)
  • rg (region override)
  • tz (time zone)
Heap Allocation on Alternative Memory Devices (JEP 316)

Enable the HotSpot VM to allocate the Java object heap on an alternative memory device, such as an NV-DIMM, specified by the user.

Experimental Java-Based JIT Compiler (JEP 317)

Enable the Java-based JIT compiler, Graal, to be used as an experimental JIT compiler on the Linux/x64 platform.

Root Certificates (JEP 319)

Provide a default set of root Certification Authority (CA) certificates in the JDK.

Time-Based Release Versioning (JEP 322)

Revise the version-string scheme of the Java SE Platform and the JDK, and related versioning information, for present and future time-based release models.

The version number ($VNUM) consists of $FEATURE.$INTERIM.$UPDATE, e.g., 10.0.1. It is targeted for six-month release every March and September.

Changes to Standard Class Libraries

Some of the changes are:

java.util.List, java.util.Map, java.util.Set

Each of these interfaces gains a new static method, copyOf(Collection). These return an unmodifiable List, Map or Set containing the elements of the given Collection, in its iteration order.

java.util.Formatter, java.util.Scanner

Both these classes have three new constructors, all of which take a charset in addition to other arguments.

Many more

REFERENCES & RESOURCES