JDK 15 has been released to the public Java developer community since September 15th 2020, as of 6-month release cadence. In this article, I would like to share with you some new features in Java 15 that are most important to developers.

 

1. Sealed Classes and Interfaces (preview)

This language feature allows programmers to specify exactly known subtypes of a class or interface. In other words, restrict the extension of a superclass to only some known subclasses – prevent arbitrary (unknown) subclasses. Let’s take an example as below:

public sealed class Shape permits Rectangle, Circle, Triangle { }
Here, the Shape class permits exactly only 3 subclasses that are Rectangle, Circle and Triangle. These subclasses are known at compile time, and no other classes can extend the Shape class.

And a subclass of a sealed class should be declared either final, sealed or non-sealed. For example, the Rectangle class is declared as follows:

public final class Rectangle extends Shape { }
Here, no classes can extend the Rectangle class because it is declared as final.

And the Circle class is declared as follows:

public sealed class Circle extends Shape permits Sphere { }
This means the Circle class is sealed and it permits Sphere as the only one subclass.

And the Triangle class is declared as below:

public non-sealed class Triangle extends Shape { }


Here, the Triangle class is non-sealed, which means it opens extension for any unknown classes.

The similar rules applied for sealed interfaces. This feature will introduce 3 potential new keywords for the Java language: sealed, permits and non-sealed. Sealed classes and interfaces are still in preview mode in Java 15, which requires further feedback from developer community before it becomes a standard feature in future release of Java.

For greater details of sealed classes, you can read JEP 360 on java.net.

 

2. Hidden Classes

Java 15 introduces hidden classes, which are classes that cannot be used directly by other classes. Hidden classes are intended for use by frameworks that generate classes at run time and use them indirectly via reflection.

If you use frameworks like Spring and Hibernate, you probably know that they generate lots of dynamic classes at run time. For example, Hibernate generates dynamic classes as proxies of entity classes and Spring generates dynamic classes for transaction management and repositories implementations.

I think the main motivation behind hidden classes is to generate short-live dynamic classes which can be unloaded completely from JVM when they are no longer used – avoid unnecessary increased memory footprint.

Study JEP 371 for detailed specification of hidden classes.

 

3. Pattern matching for instanceof (second preview)

This feature was introduced in JDK 14 and it is still under review in JDK 15. In Java, programmers are used to use the instanceof operator in a very verbose way:

if (obj instanceof String) {
    String s = (String) obj;
    // use s
}
The statement that casts the object obj to the variable s of type String is unnecessarily verbose. But now, programmers don’t have to write that statement anymore, like this:

if (obj instanceof String s) {
    // s can be used directly here
}
Furthermore, we can add expressions to the instanceof operator as shown below:

if (obj instanceof String s && s.length() > 10) {
    // use String s if its length > 10
}
So I think this feature will make the use of instanceof operator easier, more flexible and more powerful. And hope it will become standard feature in Java 16. For details, you can read the JEP 375 page.

 

4. Text Blocks (Standard)

Another frustrating issue when writing Java code is the declaration of String literals that span on multiple lines. Let’s take a classic example:

@Query("SELECT p FROM Product p WHERE p.name LIKE %?1% "
		+ "OR p.shortDescription LIKE %?1% "
		+ "OR p.fullDescription LIKE %?1% "
		+ "OR p.brand.name LIKE %?1% "
		+ "OR p.category.name LIKE %?1%")
public Page<Product> findAll(String keyword, Pageable pageable);
Though IDEs can help writing multi-line String literals easily, the concatenation operators and escape sequences make the String difficult to read and also maintenance is cumbersome.

Feel the pain of developers, Java 15 finally supports text blocks feature that allows programmers to write multi-line String literals much easier – almost avoid the need of concatenation and escape sequences. The above code can be rewritten using text blocks as shown below:

@Query("""
	SELECT p FROM Product p WHERE p.name LIKE %?1% \
	OR p.shortDescription LIKE %?1% \
	OR p.fullDescription LIKE %?1% \
	OR p.brand.name LIKE %?1% \
	OR p.category.name LIKE %?1%
	""")
public Page<Product> findAll(String keyword, Pageable pageable);
Note that the backward slash character (\) tells Java compiler to continue the String literal on the next line (hence not inserting new line characters).

So I think this is the most useful feature in Java 15. We now can embed SQL, HTML, JSON, XML, etc in Java code easily and avoid the maintenance headache.

Read this article to understand how to use text blocks in depth.

 

5. Record (second preview)

Java is also very well-known for its verbosity when it comes to “data carrier” classes (e.g. domain model/entity/POJO). Let’s take a classic entity class written in Java:

public class Product {
	private Integer id;
	private String name;
	private float price;

	public Product() {
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	@Override
	public int hashCode() {
		// ...
	}

	@Override
	public boolean equals(Object obj) {
		// ...
	}
	
	@Override
	public String toString() {
		// ...
	}	
}
As you can see, the field’s getters and setters, and equals(), hashCode(), toString() methods are cumbersome and it will be frustrating when the fields and entity classes growing.

Now with the record feature, the above lengthy class can be declared as simple as below:

record Product (Integer id, String name, float price) { }
I think this feature will be very helpful for developers. However, it is still under review (second preview) in Java 15, and it may become standard feature in Java 16.

Besides the new features above, JDK 15 also includes the following updates, for your reference:

 

Edwards-Curve Digital Signature Algorithm (EdDSA)

EdDSA is a modern cryptography algorithm which provides improved security and performance compared to other algorithms. JDK 15 implements EdDSA into its crypto API, so programmers can use EdDSA without having to use a third-party library.

 

Reimplement the Legacy DatagramSocket API

The DatagramSocket API was implemented in the early days of JDK when IPv6 was still under development so it has several issues. That’s why JDK 15 replaces the underlying implementation of this API with simpler and more modern implementations that are easy to maintain and debug.

 

Foreign-Memory Access API (Second Incubator)

This API was first introduce in JDK 14 and is still in incubator in JDK 15. It aims at providing an API that allows programmers to safely and efficiently access the memory area that is outside of Java heap.

 

Remove the Nashorn JavaScript Engine

JDK 15 finally dropped Nashorn – a Javascript engine running on JVM. That means developer is no longer able to write and run Javascript on JDK 15 and future releases. The suggested alternative is GraalVM which supports latest Javascript specification and provides better performance.

 

Deprecate RMI Activation for Removal

RMI is a very old technology which could be used in the early days of Internet and World Wide Web. Today, RMI is no longer suitable and almost dead so it’s naturally to be removed from JDK.

And as usual, new JDK release always includes performance improvements for Java Virtual Machine and garbage collector. For the complete list of features in JDK 15 with full detailed description, I recommend you to visit the OpenJDK 15 project page.

To see the code of new features in Java 15 in action, watch the video below:

 

You may be also interested in:

 


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment