Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Features in Java 10

DZone's Guide to

Features in Java 10

Let's take a sneak peek int what's coming in Java 10, including local variable type inference and APIs for unmodifiable collections.

Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

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.

Image title

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

Image title

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

Image title

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.

Image title

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

Image title

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.

Restrictions

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.

Image title


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.

Image title

APIs for Creating Unmodifiable Collections

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

Example:

Image title

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

Example:

Image title

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);  

Image title

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;
} 


Relax the Removal of Finalize Methods in FileInputStreams and FileOutputStreams

The resources used by the stream should be cleaned by calling the close method or using the try with resources feature. In the future, classes that extend the FileInputStream or FileOutputStream should modify the cleanup approach such as Cleaner instead overriding the finalize method.

Removal of Deprecated Methods

The deprecated java.security.{Certificate, Identity, IdentityScope, Signer} classes are marked for removal by setting the value of forRemoval=true in a Deprecated annotation.

The deprecated java.security.acl is also now marked for removal by the setting the value of forRemoval=true in a Deprecated annotation. A few fields and methods in java.lang.SecurityManager are also marked for removal in this version.

javax.security.auth.Policy is also marked for removal and features of the class are now available in java.security.Policy.

com.sun.security.auth.** classes, which were marked for removal in JDK 9 by setting the value of forRemoval =true in a Deprecated annotation, have been removed

Parallel Full GC for G1

Java 9 made G1 as the default, and in Java 10, parallel full Garbage Collection support for G1 will improve the worst-case latencies. It parallelized the mark-sweep-compact algorithm.

The number of threads in parallel can be controlled by the XX:ParallelGCThreads option.

Removal of javah  

The native header tool is now removed and we can generate the native header using the Java compiler with the -h option

All the code is executed using JShell.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java 10 ,java ,type inference ,for loop ,collections

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}