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 |