Features in Java 15

Patrycja Banasik

Java 15 is the latest release. It was released in September 2020, several new features were added. The next LTS release – Java 17 is planned in September 2021.

Sealed classes

Sealed classes give better control on inheritance. They can permit or restrict subclasss inheritance. To mark the class as sealed the keyword sealed is used, to permit inheritance on some subclasses the keyword permits is used. It is mandatory to mark all subclasses by keywords: sealed, non-sealed, or final, it ensures that class hierarchy is known by the compiler. The keyword final is used to ensure that other classes cannot use this class as subclass. The keyword non-sealed is used to remove the seal from hierarchy, it allows extending the class itself by unknown subclasses. In Reflection API two methods were added to support the new feature: getPermittedSubclasses and isSealed.

 public sealed class Building permits Library,PostOffice,Arena{
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
public final class Library extends Building{
}
public non-sealed class Shop extends Building{
private String manager;

    public String getManager() {
        return manager;
    }

    public void setManager(String manager) {
        this.manager = manager;
    }
}

Records (Second Preview)

Originally records were added to Java 14, but in Java 15 some of their features were modified. Record classes are immutable data objects which are created via the record keyword, they reduce significantly the amount of code. In Java 15 the possibility of using record classes with sealed types was added.

public sealed interface RepairService permits ShoesRepairService{
}

public record ShoesRepairService(List<string> shoesTypes) implements RepairService {
}

Hidden classes

Hidden classes are created at runtime, they cannot be linked by other classes, they cannot be discovered via reflection, they have a limited lifecycle. New defineHiddenClass method was implemented to create such classes.

Edwards-Curve Digital Signature Algorithm

New digital signature scheme using the Edwards-Curve Digital Signature.

Text Blocks

Text blocks were added as a standard feature, previously they were experimental features. They allow using multi-line text inside the Java code, it is useful for example for HTML, SQL.

Removal of Nashorn JavaScript Engine

Two modules were removed:

  • jdk.scripting.nashorn
  • jdk.scripting.nashorn.shell

Z Garbage Collector and Shenandoah Garbage Collector

Z Garbage Collector and Shenandoah Garbage Collector were added as a standard feature, previously they were experimental features. The G1 collector will remain the default garbage collector.

Solaris and SPARC Ports

The source code and build support for the Solaris/SPARC, Solaris/x64, and Linux/SPARC ports were removed from release. They were deprecated in Java 14.

Legacy DatagramSocket API

java.net.DatagramSocket and java.net.MulticastSocket APIs were reimplemented.

Sources

Summary

Java 15 is the latest small release which provides new features: sealed classes, hidden classes, new digital signature scheme. Some previous experimental features were added as standard features.

Poznaj mageek of j‑labs i daj się zadziwić, jak może wyglądać praca z j‑People!

Skontaktuj się z nami