Features in Java 10 (Part 1)

03-06 16:01

Java 9 has changed Java's structure by introducing modular development (Project Jigsaw). Meanwhile, Java 10 will add flavors by introducing new features of its own. A few features are explained below, with snippets.

Local Variable Type Inference

Java 7 introduced the diamond operator:

List list=new ArrayList<>();  

But even though we have a diamond operator, there is still a lot of boilerplate code. So, Java 10 introduced var as a reserve type name to reduce verbosity. It can be used as a variable, method, and package name, but we cannot use it as a class or interface name.

The advantage is that we no need to explicitly declare the variable type, so it reduces boilerplate code and increases readability.

In the above example, we have used the var in various examples of local variable type inference.

The above example describes that we can use the var reserved keyword as a variable name and method names.

The above example describes that var is a restricted local variable type and cannot be used for class and interface names. That should be very helpful in reducing boilerplate code.

I personally don't prefer this type inference much due to cases where we are not sure which datatype it is implying.

The types are inferred at compiletime and not runtime, so it will not affect performance.The resulting bytecode is the same as that with an explicit type declaration.


We cannot use the var on a variable without initializers.

var i; // invalid

Also, var is not allowed in a compound declaration.

var i , j =0; //invalid

And we cannot initialize with a null value for var-type variables:

var i=null; // invalid

Optional.orElseThrow() Method

Java 10 introduced the new method orElseThrow to the Optional class. It is preferred as an alternative to the existing get method. An example below describes the working of the orElseThrow() method.

Removal of Runtime.getLocalizedInputStream and getLocalizedOutputStream Methods

Java 1o removed the getLocalizedInputStream and getLocalizedOutputStream methods from the Runtime class, as they had no uses with respect to an internalization mechanism.

APIs for Creating Unmodifiable Collections

List.copyOf, Set.copyOf, and Map.copyOf methods are added to create the instance from an existing instance.


Also, toUnmodifiableList, toUnmodifiableSet, and toUnmodifiableMap have been added to the Collectors class in the Stream package, which will produce unmodifiable collections.


Byte Code Generation for Enhanced For Loops

The following example describes the generation of the following lines of code until Java 9.

List data = new ArrayList<>();for (String b : data);  

In Java 10, iterator variables are declared outside for loops and initialized to the null value immediately once the operation is over, so GC can get rid of unused memory.

    Iterator iterator = data.iterator();
    for (; iterator.hasNext();) 
        String b = (String)iterator.next();
    b = null;
    iterator = null;

All the code is executed using JShell.

标签: Java
© 2014 TuiCode, Inc.