As you probably already know, Java 10 was released the past 20th of March following the new tight six-month release schedule. This version comes just after the Java 9 release on September 2017 and marks this past release obsolete. Same will happen to Java 10 on September this year, when the release of Java 11 will mark this one obsolete.
Java 11 will be released as a long term support (LTS) version and will have a much longer lifespan, but until then, let’s take a look at some of the new features in Java 10.
Java 10 introduces the new time-based release versioning (JEP 322) which is a recast of JEP 223 delivered on Java 9 to allow the new release model. From now on version numbers will follow this pattern:
Every six months a new feature version will be released, every three years starting on September 2018 the feature release will be a long-term support release.
Java 10 includes a bunch of new features, but for most of us the most apparent feature is JEP 286: Local-Variable Type Inference. This features improves the developer experience by removing the boilerplate, necessary until now, to declare local variables expressing the full type fo the variable.
1var shouldBeString = "Hello Java 10"; 2assertTrue(String.class.isInstance(shouldBeString));
From now on, local variables can be declared using the reserved word
var, type safety for the variable will be preserved by inferring the variable when its value is first assigned.
So the following code will throw a compilation failure error:
- 296: Consolidate the JDK Forest into a Single Repository
- 304: Garbage-Collector Interface
- 307: Parallel Full GC for G1
- 310: Application Class-Data Sharing
- 312: Thread-Local Handshakes
- 313: Remove the Native-Header Generation Tool (javah)
- 314: Additional Unicode Language-Tag Extensions
- 316: Heap Allocation on Alternative Memory Devices
- 317: Experimental Java-Based JIT Compiler
- 319: Root Certificates
orElseThrow() method has been added to the
Optional class to replace the standard
get() method as the name is confusing because developers won’t expect a getter to throw an exception. Form now on this is the preferred alternative and the
get() method will be deprecated in future releases.
1final Optional<String> optional = Optional.ofNullable("Optional improvement in Java 10"); 2final String string = optional.orElseThrow(); // Returns OK 3final Optional<String> optional = Optional.ofNullable(null); 4final String string = optional.orElseThrow(); // Throws NoSuchElementException
1Stream.of(1, 2, 3).collect(Collectors.toUnmodifiableSet()); 2Stream.of(1, 2, 3).collect(Collectors.toUnmodifiableList()); 3Stream.of(1, 2, 3).collect(Collectors.toUnmodifiableMap(Function.identity(), Function.identity()));
You can find the full source code for this article with tests for the new features and enhancements at GitHub.