Java 10 Features with Examples

Since its first release in 1995, Java has evolved and many versions have been released. The latest version of Java is Java 10. This article gives you an insight into what significant features java 10 possesses and how it differs from its older versions.

Need of Java 10

Before learning about what new features Java 10 has brought to you, let us first understand why we need a newer version of this language. Java is widespread and is used by an abundance of developers across the continents. It brings money to the table, not just in millions, but in billions and trillions through various features that it provides.

Oracle considers positioning java as the #1 programming language even in the future decades. And without continuous upgrades, this wouldn’t be possible. New innovations and the fast-growing technological world demand better features than what is already available. Now, let us see how Java 10 suffices the expectations.

Features of Java 10

1. Local Variable Inference:

The concept of type inference on the local variables has been limited to only certain scenarios. As it was unavailable in other versions of java, the developers have taken initiative on adding this feature in Java 10.

In a for loop, if there are indexes for the local variables, we can implement the local variable type inferences. The reserved keyword ‘var’ has been introduced to obtain the local variables and infer the type to it.

2. Updation of version number:

In Java 10, we can update the version number so that the developers can be aware of the version that they are using. Some of the reserved words that we use to implement this functionality are

  • $FEATURE. – It relates to the feature release versions.
  • $INTERIM. – It relates to the non-feature release versions.
  • $UPDATE. – It includes any updates regarding the security or bugs
  • $PATCH. – It includes the perilous issues along with their updated versions.

3. Merging the JGK Forest into a single repository:

All the repositories present in the JDK forest are combined into a single repository.

4. Parallel Full GC for G1:

The introduction of a Garbage Collector in Java 9 has led to the advancement of garbage collection in Java. Due to the latency of concurrent threads, this feature has a backlash. As a result, it made the developers wait for scads of time for the application to restart.

In Java 10, this demerit was abolished by implementing parallel collection. The G1 algorithm does not recollect the memory of the previous outputs and makes it parallelized.

In addition to it, a common Garbage Collector Interface is added that helps in extending new features to the Garbage Collector without the need to change it in the database.

5. Unmodifiable Collections:

i. copyOf():  

The libraries java.util.List, java.util.Map and java.util.Set contain a new static method copyOf(Collection). We can use this method to return the unmodifiable copy of the present Collection.

Here is a snippet of how to use this method:

@Test(expected = UnsupportedOperationException.class)
public void whenModifyCopyOfList_thenThrowsException() {
List<Integer> copyList = List.copyOf(anotherList);   
copyList.add(6);
}

If we modify this collection, the java.lang.UnsupportedOperationException runtime exception raises.

ii. toUnmodifiable*():

The library java.util.stream.Collectors contain new methods to collect a Stream into an unmodifiable List, Set, or Map.

Here is a snippet of how to use this method:

@Test(expected = UnsupportedOperationException.class)
public void whenModifyToUnmodifiableList_thenThrowsException() {
    List<Integer> evenList = someIntList.stream()
      .filter(i -> i % 2 == 0)     
.collect(Collectors.toUnmodifiableList());   
evenList.add(6);
}

If we modify this collection, the java.lang.UnsupportedOperationException runtime exception raises.

iii. The libraries java.util.optional, java.util.OptionalDouble, java.util.OptionalInt, and java.util.OptionalLongeach consists of a new method orElseThrow().

This method contains no arguments and throws the NoSuchElement if the given value is not present.

Here is a snippet of how to use this method:

@Test
public void whenListContainsInteger_OrElseThrowReturnsInteger() {
    Integer evenNum = myList.stream()
      .filter(i -> i % 2 == 0)
.findFirst()
.orElseThrow();
is(evenNum).equals(Integer.valueOf(2));
}

This method acts as an alternative to the get() method that is currently used.

6. Root Certificates:

The cacerts keystore was designed to store root certificates that create trust over the certificate chains. As it was left empty, the critical components like the TLS did not function in a default manner under OpenJDK builds.

In Java 10, Oracle has Open-sourced the root certificates in its Java SE Root (Certification Authority) CA program that contains the default set of root CA certificates.

This helps the developers to surf through the OpenJDK platform easily. It also minimizes the differences between those builds and the Oracle JDK builds.

7. Application Data-Class Sharing:

The current Class Data Sharing (CDS) can be extended to enhance startup and footprints. The CDS lets the application classes be present inside the archive that is shared.

8. Graal Compiler:

The Graal compiler was introduced in Java 9. It is further improvised in Java 10. This compiler supports Polyglot language interpretation and Ahead Time Compilation (AOT). As the name suggests, the AOT lets the user know the result before it is even compiled. It also contains live code optimization that saves the developers’ time in fixing the bugs and executing the program multiple times.

The older Java compilers were primarily written in C++, whereas Graal is written in java.

9. Unicode Extensions:

We already have numerous Unicode extensions in the present Java versions. Furthermore, extensions like ‘cu’, ‘fw’, ‘rg’, and many others extensions are present in the java.util.Locale library.

Extensions that denote different currency types, weekdays, and regions are also present.

Modifications of APIs take place in a way to accommodate these extensions for their effective functioning.

In addition to these, Java 10 also supports the BCP47 language tags.

10. Heap Allocation on other Memory Devices:

Due to the introduction of newer versions and their enhancements, the storage and memory requirements of the applications have been raised. Finding the memory space in a device according to the system’s requirements is a tedious task.

Fortunately, Java 10 has a new concept known as memory architecture. In this scenario, we can use a Hotspot VM as an alternative memory device if need be. This feature allows the HotSpot VM to provide the Java object heap on a different memory device. For example, an NV-DIMM by a user.

11. The handshake method:

Multithreading is one of the most outstanding features of Java. Sometimes, a single thread would be running unwantedly. To stop this thread without affecting the performance of the other threads, we can use the handshake method.

This is a callback for the Java Thread when the Thread is in its safest mode. The handshake method does not require a global safe point. Therefore, we can stop individual threads without affecting the other threads at once.

Removals and Depreciations in Java 10:

Java 10 has not only introduced many features, but it has also removed some of the features from its older versions that were found to be inefficient.

1. Command Line options and Tools:

In Java, we cannot use the javah tool that generates the C header files and source files. In case we need to implement the native methods, we can use the javac -h command.

2. Policytool

Another tool names policytool was present to create files regarding the policies and manage them. This tool is absent in Java 10. As a replacement, we can use the simple text editor to serve the same purpose.

3. The java -Xprof

This option has been removed from the latest Java version. We can profile the running program and send the profiling data to standard output. In the recent version, we can use the jmap tool instead.

Java APIs:

Removal of various older APIs and upgrading it with new APIs are found in Java 10. An assistant API is present that informs us about the indexes and nodes’ upgrades. A Keystore safeguards the APIs. The API’s key management restricts unauthorized access.

A certain level of security is lifted when users with specific attributes try to access the keystore. The Attribute-Based Access Control (ABAC) takes care of it. Document-level security is also present. It also lets the users mark the errors in the server to avoid repetition of those mistakes.

Java Time-Based Release Versioning:

From Java 10, Oracle is implementing the format of the time-based release of Java. This includes:

1. Releasing a new Java every six months: Oracle releases a couple of Java versions every year. This commences with the March 2018 release of JDK 10. The second release in 2018 was in the month of September. They vary with one or two significant features.

2. Supporting the feature for only six months: Until the next version releases, support for the released version is available for six months.

3. LTS: It denotes the Long-term Support releases and their tenure is for three years. The next version of Java 11 will be an LTS release.

Conclusion

I hope this article has helped you understand the new features that Java 10 contains. You can download this latest version of Java on your laptop and make use of its functionalities. Check out our other articles on Java that help you deep dive into it.

Leave a Reply

Your email address will not be published. Required fields are marked *