Java 10 New Features

After looking into the new features in Java 9, I wanted to continue my series of Java new features with Java 10.

Local-Variable Type Inference

Let’s recap a bit about this topic. Earlier than Java 7, we had to declare the types as:

Map<String, String> map = new HashMap<String, String>();

Then, Java 7 brought the Diamond Operator where we don’t need to provide the right type:

Map<String, String> map = new HashMap<>();

What if we don’t need to know about the left side type? Embrace the local-variable type inference. This feature is native in Javascript and has been available in C# from a long while ago …

var map = new HashMap<String, String>();

It might look like a very unnecessary, however when working with Streams, sometimes it’s a bit frustrating to infer the type your self:

var list = stream.collect(Collectors.toList());

APIs for Creating Unmodifiable Collections

List.copyOf, Set.copyOf and Map.copyOf methods have been added in the Collections API in order to make unmodifiable copies:

var origin = new ArrayList<Integer>();
origin.add(1);
origin.add(2);
var copy = List.copyOf(origin);
copy.add(3); // unsopported operation exception

Also, the Collectors API have been updated to create unmodifiable collections:

var list = stream.collect(Collectors.toUnmodifiableList());

Parallel Full GC for G1

We already talked about the new default garbace collector since Java 9: G1GC. In Java 10, this garbage collector has been refactored to improve the worst-case latencies. More in here.

Minor Changes

  • New Optional.orElseThrow() Method
  • Removal of Deprecated Methods
  • Removal of Runtime.getLocalizedInputStream and getLocalizedOutputStream Methods
  • Removal of javah
[ Java ]