Java 15 Features

Java 15 released in Sep 2020. It is a non-LTS version that added a couple of new features and also removed some existing features.In this article, we will look at Java 15 Features.We will see what are the new features that are coming in Java 15.

Java 15 Features

Here are some of the main Java 15 features that are available or removed with JDK 15

  1. JEP 360 – Sealed Classes and Interfaces.
  2. Hidden Classes (JEP 371)
  3. JEP 375
  4. JEP 378 – Text Block
  5. JEP 339 – EdDSA Algorithm
  6. Pattern Matching for instanceof.
  7. Removed Nashorn JavaScript Engine
  8. Text Blocks (JEP 378)
  9. Garbage Collectors

1. JEP 360 – Sealed Classes and Interfaces

Before Java 15, there was no fine-grained control over the inheritance. To put in simple terms, there were no restrictions on classes or interfaces on which class can be inherited.

  1. Before Java 15, any class can implement a public interface.
  2. A public class can be extended by any other class unless classes are declared as final.

With JEP 360, we can declared classes and interfaces as sealed using the sealed modifier.With the sealed modifier, we can restrict which other classes or interfaces can extend or implement them. This feature takes a more declarative approach than the standard access modifier.

sealed class User permits Admin, Employee, Customer, Manager{
}

final class Admin extends User{}
sealed class Employee extends User permits Manager, ProductManager{}
non-sealed class Customer extends User{}

final class Manager extends Employee{}
final class ProductManager extends Employee{}

2. Hidden classes (JEP-371)

JEP-371 introduced a new form of a class called hidden class. It is a class that can’t be used directly by the byte-code of other classes is called hidden class. Many platforms use dynamic class generation. Hidden classes are intended to be used by such platforms. These platforms use reflection to use dynamic classes indirectly. But hidden classes can’t be discovered through reflection. We can add a hidden class as a member of the access control nest, and it can also be unloaded independently of any other classes in that access control nest. 

JEP-371 also deprecated sun.misc.Unsafe::defineAnonymousClass and they will remove it in a future release. Note that not all the functionalities of defineAnonymousClass are supported by this change.

3. Second preview for pattern matching for instanceOf(JEP-375):

Pattern matching for instanceOf is added as a preview feature in JDK 15. This change was added in JDK 14, and they keep it the same with no changes in JDK 15. The primary intention of this change is to get more feedback on this feature.In Java, we can use the instanceOf operator to check if some variable is an instance of any other class. For example:

if(givenStr instanceOf String){ 
   String str = (String)givenStr; 
}

Here, first, we are checking if givenStr is an instance of String or not. If it is, we are casting the value to String and storing that in str.With this change; we don’t have to do the instance check and casting separately. We can simply do that in one check:

if(givenStr instanceOf String str){ 

}

This block basically does the same thing. But, instead of writing two lines, we can check the instance and do the casting in one line. 

4. Text Blocks (JEP-378):

Text blocks were targeted in JDK 12, in JDK 14 it was added as a preview feature and finally, in JDK 15 it is added as a permanent feature. Writing multiple line strings is difficult in Java. It is not the best way that we write multi-line strings in Java. If you compare it with other programming languages, you find that in most languages, we can write multi-line strings easily using no escape sequence. Text blocks improve the way to write a multi-line string by avoiding most of the escape sequences.

Text blocks are used to write multi-line strings, and it is opened and closed using three double quotes ("""). Let’s look at the below string:

"<html>\n"+ 
" <body>\n"+ 
" </body>\n"+ 
"<html>\n";

Using text block, we can write this string as below. It makes the code clean, easy to understand, and easy to maintain.

""" 
<html> 
<body> 
</body> 
</html> 
""";

5. Second preview of Record (JEP-359):

Record is like data class in <a aria-label="Kotlin (opens in a new tab)" href="https://kotlinlang.org/" target="_blank" rel="noreferrer noopener" class="rank-math-link">Kotlin</a>. The Record was added as a preview in JDK 14. In JDK 15, they again kept it as a preview. This is added to remove boilerplate code from a POJO class. Let’s consider the below class:

public class Student{ 
  private String name; 
  private Integer id; 
  private float age; 
}

To complete this class, we need to create 1 constructor, 2 getters and setters for each variable, and other overriding methods i.e. equals(), toString(), and hashCode(). These methods will keep increasing if we keep adding variables to this class. Using records, we can use these methods without needing to write them in the class explicitly. We can do this as below:

record public Student(Integer id, float age, String name){ }

Records are designed to be used mostly for data holder classes, and we should avoid defining any other methods of record classes. This is still a preview feature in JDK 15, but there is a high chance that it will be added permanently in JDK-16

6. Edwards-Curve Digital signature algorithm(EdDSA)(JEP 339)

EdDSA or Edwards-Curve Digital signature algorithm is a modern cryptographic algorithm used for signature schemes. This algorithm has many advantages over other algorithms. This algorithm provides the implementation of the cryptographic signature, as provided in REF 8032. This signature scheme doesn’t replace the old signature scheme ECDSA, but developers can use it on new projects. ECDSA and EdDSA both have the same security strength, but performing EdDSA is better than ECDSA.

7. Remove the Nashorn JavaScript Engine (JEP 372)

Nashorn JavaScript Engine, it’s APIs and jjs tools are removed with JEP-371. Nashorn JavaScript Engine was introduced in JDK 8 to replace Rhino scripting engine. It was deprecated in JDK 11, and now it is removed. This change will remove two modules :

  1. - jdk.scripting.nashorn. This module contains two packages: jdk.nashorn.api.scripting and jdk.nashorn.api.tree

2. – jdk.scripting.nashorn.shell. This module contains the jjs tool.

8. Legacy DatagramSocket API Reimplementation (JEP 373)

This change replaces the underlying implementations of the java.net.DatagramSocket and java.net.MulticastSocket APIs. It changes the implementations of these APIs that will be easy to maintain and debug. The old implementation is dated back to JDK 1.0 which contains a mix of Java and C code which is hard to maintain.

9. A Scalable low-latency garbage collector (JEP 377)

This change added the Z Garbage collector. Z Garbage collector was added to JDK 11 first as experimental. The development and bug fix for this garbage collector was on the side since then. This JEP doesn’t include any testing-related changes for this garbage collector. All changes are kept the same as before.

10. Shenandoah, A Low pause-time garbage collector (JEP 379):

Shenandoah garbage collector was in experimental mode. It is changed to a production feature. Shenandoah was integrated into JDK 12 as experimental. This JEP is not a sign to change the default garbage collector, G1. To enable Shenandoah, the below command need to be used for JDK below 15:

java -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC

From Java 15, we don’t need the experimental flag, i.e. we can enable it by:

java -XX:+UseShenandoahGC

11. Foreign-memory access API (Second Incubator) (JEP 383):

Foreign memory API is used to safely access foreign memory outside the Java heap. This change was proposed and targeted in JDK 14 as an incubating API. This JEP includes improvements on this API. It is still an incubating API. This is a safe and efficient API for accessing foreign memory. This API is provided in a module and package named jdk.incubator.foreign. 

12. Disable and deprecate biased locking (JEP-374):

Biased locking is disabled by default with this JEP. Other command-line options are deprecated. Biased locking is used in HotSpot virtual machines. It is an optimization to reduce the overhead of uncontested locking. Biased locking was available before JDK-15. After this JEP, it is deprecated and disabled by default. It can be enabled by using XX:+UseBiasedLocking when HotSpot is started. This JEP says that XX:+UseBiasedLocking option and other configurations will be deprecated.

13. Deprecate RMI Activation for Removal (JEP-385):

Since Java 8, RMI Activation is optional. This JEP will deprecate the RMI activation mechanism and it will be removed in a future release.The changes are reflected on java.rmi.activation package. It adds @Deprecated(forRemoval = true) to all public classes and interfaces in this package. It also adds a deprecated Javadoc tag to the package declaration of java.rmi.activation.Other parts of RMI are not changed by this JEP.

15. Remove the Solaris and SPARC Ports(JEP-381):

Solaris/SPARC, Solaris/x64, and Linux/SPARC ports were deprecated in JDK14. With this JEP, all source code specific to the Solaris operating system and SPARC architecture are removed. These changes will help the progress of many open-source projects like Valhalla, Loom, and Panama which require many operating system-related changes. 

Summary

In this post we talked about the main Java 15 Features. We saw what are some of the new features in Java 15, along with some items which are deprecated or removed from the JDK release.

Scroll to Top