What’s New in Java? Key JDK Changes from String to Private Interface Methods
This article reviews the major updates introduced in recent JDK releases, covering the shift from char[] to byte[] in String, expanded pattern‑matching in switch, removal of biased locking, G1 garbage‑collector evolution, JDK/JRE consolidation, generics specialization, and the addition of private methods in interfaces.
1. String no longer uses char[]
Before JDK 9, a String stored its characters in a char[]. Starting with JDK 9 it uses a byte[] to save memory, especially for Latin‑script characters, and introduces a coder byte to indicate whether the data is ISO‑8859‑1 (single‑byte) or UTF‑16 (double‑byte).
2. switch supports more types via pattern matching
Pattern matching, first seen in Scala and Haskell, was added to Java in JDK 14. It lets you combine type checks and casts, e.g.:
Object obj = "hello";
if (obj instanceof String str) {
System.out.println(str.length());
}In JDK 17 the switch statement also supports pattern matching:
Object obj = 10L;
switch (obj) {
case String str -> System.out.println("str: " + str);
case Integer intNum -> System.out.println("int: " + intNum);
case Long longNum -> System.out.println("long: " + longNum);
default -> throw new IllegalStateException("Unexpected value");
}3. Biased locking removed
Biased locking was an optimization that avoided synchronization when the same thread repeatedly entered a synchronized block. In practice it often did not improve performance and could cause long pauses due to safepoint pauses. Consequently it was disabled by default in JDK 15 and completely removed in JDK 18.
4. G1 garbage‑collector changes
Since JDK 9, G1 is the default collector. It no longer uses a strict young‑generation/old‑generation division; instead it splits the heap into many equal‑sized regions that can assume different roles over time.
5. JDK/JRE relationship simplified
Historically JDK and JRE were separate downloads. Starting with JDK 9 Oracle stopped providing a standalone JRE; developers now use jlink to create custom runtime images that contain only the modules required by the application.
6. Generics evolution
Java generics are implemented via type erasure, which removes generic type information at runtime. The Valhalla project aims to introduce generic specialization, preserving type information at runtime for better performance and safety, though progress is still slow.
7. Private methods in interfaces
Prior to JDK 8 interfaces could not contain implementations. JDK 8 added default and static methods, but shared code had to be duplicated or placed in static helpers. JDK 9 finally allowed private methods inside interfaces, enabling reuse of common code among default methods.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
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.
