It’s only been six months since Java 9’s release, and we can already take a look at Java 10’s updates. In this article, we will be focusing on the main new features that will be used by developers in this version.
The Type Inference of LocaL Variables
It’s the biggest change for Java 10 developers, the type inference for local variables. It occurs when using the VAR instruction and will facilitate the declaration of local variables.
Java remains a statically typed language, but when a variable is declared and then instantiated on the same line, the type declaration is redundant!
The keyword VAR will by consequence replace it. This will simplify the screen layout especially for complicated types.
Map<Integer, List<String>> mapping = … ;
Will become clearer when declared this way:
var mapping = … ;
There will be less unnecessary ceremonies and writing will be more concise.
Usage is also possible in loops and try-with-resources. The type is not visible but is always generated in the bytecode.
Nonetheless, there are some restrictions:
Other facts which require attention with the use of VAR keyword:
- The code might not be as clear without IDE;
- From now on it is important to emphasize variable names to simplify reading;
- Abstraction is limited: no possible inference to a super type or an interface. For example ArrayList<String>() , will necessarily be of type Arraylist but would have been of type List if there hadn’t been the type inference.
Other new features in Java 10 :
- A new « orElseThrow () » method on optional as an alternative to « get () »:
If the element is present on the optional, it returns it. Otherwise it throws an exception of type « NoSuchElementException« .
This is similar to using the orElseThrow(NoSuchElementException::new) method but without the exception’s instantiation ceremony.
- A new static method « copyOf(Collection) » on List, Set and Map. It allows the creation of an immutable copy of the collection passed in parameter.
You can also find:
- An improvement of the API Stream and the addition of Collectors in order to create immutable collections:
– Collectors.toUnmodifiableMap(keyFunc, valueFunc),
– Collectors.toUnmodifiableMap(keyFunc, valueFunc, mergeFunc).
In conclusion, Java 10 offers some interesting new features for developers, however given the high paced rate of releases every 6 months; we are far from the observed gap between Java 7 and Java 8. From now on, developers can have regular access to useful updates, which is basically a good thing for increasing the popularity of this language.
Oracle’s Java 10 new features: http://www.oracle.com/technetwork/java/javase/10-relnote-issues-4108729.html#NewFeature
Devoxx Conference linked to the article: https://www.youtube.com/watch?v=dYubeLiObqY