This document provides a detailed explanation of all major features introduced in Java 14 to Java 18, with examples, reasoning for introduction, and previous approaches or limitations.
Issue: Previously, creating simple immutable data classes required verbose boilerplate code.
Introduction: Records provide a concise way to declare immutable data carriers.
Example:
record Point(int x, int y) {}
Point p = new Point(5, 10);
System.out.println(p.x()); // 5Automatically provides equals, hashCode, and toString.
Issue: Type casting after instanceof was repetitive.
Example:
Object obj = "Hello";
if(obj instanceof String s) {
System.out.println(s.toUpperCase());
}Simplifies type checks and casts.
Reason: NullPointerExceptions were hard to debug.
Example:
String s = null;
s.length(); // JVM shows exactly which variable was nullExample:
int day = 2;
String type = switch(day) {
case 1, 7 -> "Weekend";
default -> "Weekday";
};Reason: Efficient memory access outside the Java heap.
Example:
// Use MemorySegment, MemoryAddress to access off-heap memoryReal-time monitoring of JVM events.
Simplifies multi-line string literals.
String html = """
<html>
<body>Hello</body>
</html>
""";Issue: Previously, restricting subclassing required documentation and runtime checks.
Example:
sealed interface Shape permits Circle, Rectangle {}
final class Circle implements Shape {}
non-sealed class Rectangle implements Shape {}Restricts which classes can extend or implement a type.
3. Hidden Classes
Allows frameworks to define classes dynamically without polluting the public API.
Enhancements and fixes to record features.
Further improvements for off-heap memory access.
Reduces pause times and improves memory management.
Records are now a standard feature.
record Employee(String name, int id) {}No longer preview; simplifies type checks.
Example:
List<Integer> list = Stream.of(1,2,3).toList();Simpler conversion from stream to list.
Continued development of sealed classes.
Reason: Efficient SIMD operations for performance-critical code.
Simplifies calling native libraries.
Encourages proper API usage, reduces internal dependency hacks.
Final standardization of sealed classes.
Example:
Object obj = "Hello";
switch(obj) {
case String s -> System.out.println(s.length());
default -> System.out.println("Unknown");
}Example:
RandomGenerator rnd = RandomGenerator.of("L64X128MixRandom");
int num = rnd.nextInt(100);Create native installers for Java applications.
Enhances JVM support for off-heap memory and native calls.
Applets, RMI Activation, and older APIs removed.
Reason: Quickly run small HTTP servers for testing.
Example:
java -m jdk.httpserver 8000All charset operations default to UTF-8, simplifying internationalization.
Enhancements to SIMD operations for high-performance code.
Allows adding executable code snippets to Javadoc.
Continued improvements on switch pattern matching.
Further refinements for off-heap memory and native function access.