Key New Features in Java 9–16: Private Interface Methods, Var, Switch Expressions, Records and More
This article provides a concise walkthrough of the most important Java 9‑16 language enhancements—including private methods in interfaces, diamond support for anonymous classes, enhanced try‑with‑resources, var type inference, switch expressions, text blocks, records, improved NullPointerException messages, foreign‑memory APIs, sealed classes and new garbage collectors—illustrated with clear code examples.
Top‑level architect shares a rapid overview of Java 9‑16 new language features, helping developers stay current with the fast‑paced JDK releases.
Java 9 : Interfaces can now contain private methods that default methods may call.
public interface TestInterface {
default void wrapMethod() {
innerMethod();
}
private void innerMethod() {
System.out.println("");
}
}Anonymous inner classes gain diamond operator support.
List
numbers = new ArrayList<>() {
// ...
};Try‑with‑resources is enhanced to allow resource variables declared outside the try block.
BufferedReader br0 = new BufferedReader(...);
BufferedReader br1 = new BufferedReader(...);
try (br0; br1) {
System.out.println(br0.readLine() + br1.readLine());
}Java 10 : Local‑variable type inference with var .
var message = "Hello, Java 10";Java 11 : var can be used in lambda parameters, allowing annotations.
List
languages = Arrays.asList("Java", "Groovy");
String language = sampleList.stream()
.map((@Nonnull var x) -> x.toUpperCase())
.collect(Collectors.joining(", "));Java 12 : Switch expressions with arrow syntax and yield for returning values.
typeOfDay = switch (dayOfWeek) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Working Day";
case SATURDAY, SUNDAY -> "Day Off";
};Java 13 : Text blocks simplify multi‑line string literals.
String json = """
{
"id":"1697301681936888",
"nickname":"空无",
"homepage":"https://juejin.cn/user/1697301681936888"
}""";Java 14 : Introduction of record types to reduce boilerplate POJOs.
public record UserDTO(String id, String nickname, String homepage) {};
var user = new UserDTO("1697301681936888", "空无", "https://juejin.cn/user/1697301681936888");
System.out.println(user.id);
System.out.println(user.nickname);
System.out.println(user.id);NullPointerException messages become more descriptive, pinpointing the exact null expression.
Map
> wrapMap = new HashMap<>();
wrapMap.put("innerMap", new HashMap<>());
boolean effected = wrapMap.get("innerMap").get("effected");
// StackTrace now shows which map entry was nullForeign‑memory access API replaces unsafe operations with safe handles.
MemorySegment segment = MemorySegment.allocateNative(200);
MemoryAddress address = segment.baseAddress();
VarHandle vh = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
vh.set(address, 10L);
segment.close();Java 15 : ZGC and Shenandoah become production‑ready GC algorithms, and sealed classes restrict which classes may extend a type.
public sealed interface Service permits Car, Truck {
int getMaxServiceIntervalInMonths();
default int getMaxDistanceBetweenServicesInKilometers() { return 100000; }
}Java 16 : Mostly consolidates experimental features from earlier releases; no major language changes.
In summary, the rapid JDK evolution introduces many syntactic and runtime improvements that developers should adopt early to write cleaner, safer, and more performant Java code.
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.