Java 15 and 16 Features

โœ… Java 15 Features (Released: Sept 2020)


1. ๐Ÿ”น Text Blocks (Standard โ€“ JEP 378)

Final version of multi-line string literals introduced as preview in Java 13/14.

Example:

String html = """
    <html>
        <body>Hello, Java 15!</body>
    </html>
    """;

2. ๐Ÿ”น Sealed Classes (Preview โ€“ JEP 360)

Restricts which classes can extend or implement a class/interface.

Example:

public sealed class Shape permits Circle, Square {}

final class Circle extends Shape {}
final class Square extends Shape {}

โœ… Useful for better control in inheritance and modeling domain logic.


3. ๐Ÿ”น Hidden Classes (JEP 371)

Classes intended for framework use only, not discoverable via reflection.

Example use case: proxies or dynamically generated code (e.g., in frameworks like Spring, Hibernate).


4. ๐Ÿ”น Records (2nd Preview โ€“ JEP 384)

Continued preview of record types, compact syntax for data-holding classes.


5. ๐Ÿ”น Pattern Matching for instanceof (2nd Preview โ€“ JEP 375)

Further refinement of instanceof pattern matching from Java 14.


6. ๐Ÿ”น ZGC Improvements (JEP 377)

  • ZGC becomes a production feature (low-latency GC)

  • Now supports heap sizes from a few MBs to multiple TBs


7. ๐Ÿ”น Removal of Nashorn JavaScript Engine (JEP 372)

The Nashorn engine was deprecated in Java 11 and fully removed in Java 15.


โœ… Java 16 Features (Released: March 2021)


1. ๐Ÿ”น Records (Standard โ€“ JEP 395)

Records became a final feature!

Example:

public record Person(String name, int age) {}

Person p = new Person("Alice", 30);
System.out.println(p.name()); // Alice

โœ… Great for DTOs and immutable objects with less boilerplate.


2. ๐Ÿ”น Pattern Matching for instanceof (Standard โ€“ JEP 394)

Now a final feature!

Example:

if (obj instanceof String s) {
    System.out.println(s.toUpperCase());
}

โœ… Cleaner and safer casting.


3. ๐Ÿ”น Sealed Classes (2nd Preview โ€“ JEP 397)

Improved from Java 15 preview.


4. ๐Ÿ”น JEP 376 โ€“ JDK Internal APIs Encapsulation (Strong Encapsulation)

Further encapsulates internal JDK APIs โ€” breaks reflection-based access unless explicitly opened.

โœ… Improves modularity and security.


5. ๐Ÿ”น JEP 338 โ€“ Unix-Domain Socket Channels

Java can now communicate using Unix domain sockets (used in inter-process communication).

Example:

UnixDomainSocketAddress address = UnixDomainSocketAddress.of("/tmp/mysocket");
SocketChannel socketChannel = SocketChannel.open(StandardProtocolFamily.UNIX);

6. ๐Ÿ”น Foreign Linker API (Incubator โ€“ JEP 389)

Allows calling native code (like C functions) from Java in a safer and more portable way.


7. ๐Ÿ”น Packaging Tool (JEP 392)

A cross-platform tool to create native installers (.msi, .pkg, .deb, etc.)

Command:

jpackage --input out/ --name MyApp --main-jar myapp.jar --type exe

โœ… Useful for desktop application deployment.


๐Ÿ”š Summary Comparison Table

Feature Java 15 Java 16
Text Blocks โœ… Final โœ…
Records ๐Ÿงช Preview 2 โœ… Final
Pattern Matching (instanceof) ๐Ÿงช Preview 2 โœ… Final
Sealed Classes ๐Ÿงช Preview ๐Ÿงช Preview 2
Hidden Classes โœ… โ€”
ZGC Enhancements โœ… โ€”
Packaging Tool โ€” โœ…
Unix-Domain Sockets โ€” โœ…
Foreign Linker API โ€” ๐Ÿงช Incubator
JDK Internals Encapsulation โ€” โœ… Stronger enforcement


Back to blog

Leave a comment

Please note, comments need to be approved before they are published.