New Features in Java 10

1. Introduction

JDK 10, which is an implementation of Java SE 10, was released on March 20, 2018.

In this article, we’ll cover and explore the new features and changes introduced in JDK 10.

2. Local Variable Type Inference

Follow the link for an in-depth article on this feature: Java 10 Local Variable Type Inference

3. Unmodifiable Collections

There are a couple of changes related to unmodifiable collections in Java 10.

3.1. copyOf()

java.util.List, java.util.Map and java.util.Set each got a new static method copyOf(Collection).

It returns the unmodifiable copy of the given Collection:

@Test(expected = UnsupportedOperationException.class)
public void whenModifyCopyOfList_thenThrowsException() {
    List<Integer> copyList = List.copyOf(someIntList);
    copyList.add(4);
}

Any attempt to modify such a collection would result in java.lang.UnsupportedOperationExceptionruntime exception.

3.2. toUnmodifiable*()

java.util.stream.Collectors get additional methods to collect a Stream into unmodifiable List, Map or Set:

@Test(expected = UnsupportedOperationException.class)
public void whenModifyToUnmodifiableList_thenThrowsException() {
    List<Integer> evenList = someIntList.stream()
      .filter(i -> i % 2 == 0)
      .collect(Collectors.toUnmodifiableList());
    evenList.add(4);
}

Any attempt to modify such a collection would result in java.lang.UnsupportedOperationExceptionruntime exception.

4. Optional*.orElseThrow()

java.util.Optional, java.util.OptionalDouble, java.util.OptionalIntand java.util.OptionalLongeach got a new method orElseThrow()which doesn’t take any argument and throws NoSuchElementExceptionif no value is present:

@Test
public void whenListContainsInteger_OrElseThrowReturnsInteger() {
    Integer firstEven = someIntList.stream()
      .filter(i -> i % 2 == 0)
      .findFirst()
      .orElseThrow();
    is(firstEven).equals(Integer.valueOf(2));
}

It’s synonymous with and is now the preferred alternative to the existing get()method.

5. Performance Improvements

Follow the link for an in-depth article on this feature: Java 10 Performance Improvements

6. Container Awareness

JVMs are now aware of being run in a Docker container and will extract container-specific configuration instead of querying the operating system itself – it applies to data like the number of CPUs and total memory that have been allocated to the container.

However, this support is only available for Linux-based platforms. This new support is enabled by default and can be disabled in the command line with the JVM option:

-XX:-UseContainerSupport

Also, this change adds a JVM option that provides the ability to specify the number of CPUs that the JVM will use:

-XX:ActiveProcessorCount=count

Also, three new JVM options have been added to allow Docker container users to gain more fine-grained control over the amount of system memory that will be used for the Java Heap:

-XX:InitialRAMPercentage
-XX:MaxRAMPercentage
-XX:MinRAMPercentage

7. Root Certificates

The cacerts keystore, which was initially empty so far, is intended to contain a set of root certificates that can be used to establish trust in the certificate chains used by various security protocols.

As a result, critical security components such as TLS didn’t work by default under OpenJDK builds.

With Java 10, Oracle has open-sourced the root certificates in Oracle’s Java SE Root CA program in order to make OpenJDK builds more attractive to developers and to reduce the differences between those builds and Oracle JDK builds.

8. Deprecations and Removals

8.1. Command Line Options and Tools

Tool javah has been removed from Java 10 which generated C headers and source files which were required to implement native methods – now, javac -h can be used instead.

policytool was the UI based tool for policy file creation and management. This has now been removed. The user can use simple text editor for performing this operation.

Removed java -Xprofoption. The option was used to profile the running program and send profiling data to standard output. The user should now use jmap tool instead.

8.2. APIs

Deprecated java.security.acl package has been marked forRemoval=true and is subject to removal in a future version of Java SE. It’s been replaced by java.security.Policy and related classes.

Similarly, java.security.{Certificate,Identity,IdentityScope,Signer} APIs are marked forRemoval=true.

9. Time-Based Release Versioning

Starting with Java 10, Oracle has moved to the time-based release of Java. This has following implications:

  1. A new Java release every six months. The March 2018 release is JDK 10, the September 2018 release is JDK 11, and so forth. These are called feature releases and are expected to contain at least one or two significant features
  2. Support for the feature release will last only for six months, i.e., until next feature release
  3. Long-term support release will be marked as LTS. Support for such release will be for three years
  4. Java 11 will be an LTS release

java -version will now contain the GA date, making it easier to identify how old the release is:

$ java -version
openjdk version "10" 2018-03-20
OpenJDK Runtime Environment 18.3 (build 10+46)
OpenJDK 64-Bit Server VM 18.3 (build 10+46, mixed mode)

10. Conclusion

In this article, we saw the new features and changes brought in by Java 10.

As usual, code snippets can be found over on GitHub.

Related posts:

Java Program to Implement Max-Flow Min-Cut Theorem
Java Program to Perform Sorting Using B-Tree
Java Program to Implement K Way Merge Algorithm
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Simultaneous Spring WebClient Calls
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Implement Hash Tables
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
HashSet trong Java hoạt động như thế nào?
Java Program to Implement WeakHashMap API
String Operations with Java Streams
Hướng dẫn Java Design Pattern – Dependency Injection
Removing Elements from Java Collections
Java Program to Implement ArrayDeque API
Java Program to add two large numbers using Linked List
Jackson – Unmarshall to Collection/Array
Java Program to Print only Odd Numbered Levels of a Tree
Deque và ArrayDeque trong Java
Java 8 and Infinite Streams
Spring Boot - Securing Web Applications
Supplier trong Java 8
Hướng dẫn Java Design Pattern – State
Java Program to Implement Strassen Algorithm
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Java Program to Implement ArrayBlockingQueue API
A Guide to Java SynchronousQueue
Java Program to Find the Longest Path in a DAG
How to Set TLS Version in Apache HttpClient
Spring Autowiring of Generic Types
Java Program to Perform Polygon Containment Test
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?