Key New Features in Java 9–16: Private Interface Methods, var, Switch Expressions, Records, Sealed Classes and More
This article reviews the most important Java language enhancements from version 9 through 16, covering private interface methods, diamond operators for anonymous classes, var type inference, improved try‑with‑resources, switch expressions, text blocks, records, refined NullPointerException messages, foreign memory access, sealed classes, and the new jpackage tool.
Introduction
Java releases have accelerated dramatically, with new language features arriving almost every six months. Understanding these changes helps developers keep up with the evolving platform and write more concise, safe, and performant code.
Java 9
Private methods in interfaces
Interfaces can now define private methods that default methods can call, allowing code reuse without exposing implementation details.
public interface TestInterface {
default void wrapMethod() {
innerMethod();
}
private void innerMethod() {
System.out.println("");
}
}Diamond operator for anonymous classes
The diamond operator (<>), introduced in Java 7 for generic type inference, is now supported in anonymous inner classes.
List
numbers = new ArrayList<>(); List
numbers = new ArrayList<>() {
// ...
};Java 10
Local‑variable type inference (var)
The var keyword lets the compiler infer the type of a local variable, reducing boilerplate.
var message = "Hello, Java 10";Java 11
var in lambda parameters
Lambda expressions can now use var for parameters, enabling annotations on inferred types.
List
languages = Arrays.asList("Java", "Groovy");
String language = languages.stream()
.map((@Nonnull var x) -> x.toUpperCase())
.collect(Collectors.joining(", "));Single‑file source‑code execution
Java 11 allows running a single source file directly without an explicit compilation step.
$ java HelloWorld.java
Hello Java 11!Java 12
Enhanced switch expressions
Switch can now be used as an expression that yields a value and supports multiple case labels separated by commas.
String typeOfDay = switch (dayOfWeek) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Working Day";
case SATURDAY, SUNDAY -> "Day Off";
};Java 13
Text blocks
Multi‑line string literals are now written with triple quotes, preserving formatting without escape sequences.
String json = """
{
"id":"1697301681936888",
"nickname":"空无",
"homepage":"https://juejin.cn/user/1697301681936888"
}""";Java 14
Records
Records provide a compact syntax for immutable data carriers, automatically generating constructors, accessors, equals , hashCode , and toString .
public record UserDTO(String id, String nickname, String homepage) { }
public static void main(String[] args) {
UserDTO user = new UserDTO("1697301681936888", "空无", "https://juejin.cn/user/1697301681936888");
System.out.println(user.id());
System.out.println(user.nickname());
System.out.println(user.homepage());
}Improved NullPointerException messages
The JVM now reports the exact variable that is null in a chain of method calls, making debugging easier.
Map
> wrapMap = new HashMap<>();
wrapMap.put("innerMap", new HashMap<>());
boolean effected = wrapMap.get("innerMap").get("effected"); // NPE shows that "effected" is nullForeign‑memory access API
A safe API replaces the unsafe sun.misc.Unsafe for allocating and manipulating off‑heap memory.
MemorySegment segment = MemorySegment.allocateNative(200);
VarHandle handle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
handle.set(segment.baseAddress(), 10L);
segment.close();Java 15
New production garbage collectors
ZGC and Shenandoah are now fully supported, offering low‑pause garbage collection.
Sealed classes
Sealed classes restrict which other classes may extend or implement them, providing a middle ground between final and unrestricted inheritance.
public sealed interface Service permits Car, Truck {
int getMaxServiceIntervalInMonths();
default int getMaxDistanceBetweenServicesInKilometers() { return 100000; }
}Java 16
Java 16 consolidates the experimental features introduced in 14 and 15, with no major language changes.
Conclusion
The rapid cadence of Java releases means many of these features quickly move from preview to stable. Early adoption can improve code readability, safety, and performance, while staying current helps avoid falling behind the ecosystem.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.