Close

JDK 23: Exploring the Latest Features and Enhancements in Java

Java Development Kit (JDK) 23 has been released, offering a range of new features and updates. This release introduces previews of various enhancements, including module import declarations, stream gatherers, structured concurrency, scoped values, and a class-file API. Additionally, several deprecated methods signal shifts in the Java ecosystem.

JDK 23 brings significant updates, including previews of scoped values, structured concurrency, and implicit class declarations. This release also provides second previews for flexible constructor bodies, stream gatherers, and a class-file API. Developers can download JDK 23 directly from Oracle’s website.

Key Features and Previews in JDK 23

  1. Scoped Values: Building on previews from JDK 21 and JDK 22, scoped values allow methods to share immutable data with their callees within the same thread and with child threads. This approach is more efficient than thread-local values, especially when used alongside virtual threads and structured concurrency. A new functional interface in JDK 23’s scoped values enables the Java compiler to infer the possibility of checked exceptions, leading to the removal of the ScopeValue.getWhere method.
  2. Deprecation of sun.misc.Unsafe: The memory access methods in sun.misc.Unsafe have been deprecated in JDK 23, signaling a move towards more secure and supported APIs like VarHandle and the foreign function and memory API. Although these methods will be removed in future releases, some aspects of sun.misc.Unsafe that are not related to memory access will be deprecated separately.
  3. Structured Concurrency: With its third preview in JDK 23, structured concurrency aims to simplify concurrent programming by treating related tasks across different threads as a unified task group. This enhancement facilitates error handling and cancellation processes, helping to prevent common concurrency issues such as thread leaks and cancellation delays.
  4. Flexible Constructor Bodies: This feature receives a second preview in JDK 23. It allows constructors to initialize fields before explicitly calling a superclass constructor. This change gives developers more flexibility when writing constructors and ensures that class instantiation follows a top-down order.
  5. Implicitly Declared Classes and Methods: This feature simplifies Java for beginners by allowing them to write basic programs without understanding complex language features. JDK 23 introduces enhancements where these classes automatically import three static methods for console I/O and import all public top-level classes and interfaces from packages exported by the java.base module.

Additional Enhancements

  • Module Import Declarations: As a preview feature in JDK 23, this enables succinct import of all packages exported by a module. It simplifies the use of modular libraries and allows entire modules to be imported, making it easier for developers to utilize third-party libraries without delving into package hierarchies.
  • Markdown Documentation Comments: JDK 23 allows developers to write JavaDoc comments using Markdown, making documentation easier to read and write. This feature aims to support Markdown syntax alongside existing JavaDoc tags and HTML elements, enhancing the overall readability of API documentation.
  • Z Garbage Collector (ZGC): JDK 23 switches the default mode of ZGC to the generational mode, enhancing Java application performance by focusing on frequent collection of young objects. The non-generational mode is now deprecated and is expected to be removed in future releases.
  • Vector API: Continuing its incubation since JDK 16, the Vector API allows developers to express vector computations that are compiled into optimized instructions at runtime. The API aims to be clear, concise, and performant across different CPU architectures, aligning with Project Valhalla’s value object enhancements.
  • Stream Gatherers: This enhancement to the stream API supports custom intermediate operations, making stream pipelines more flexible and expressive. The feature allows manipulation of streams of infinite size, offering more options for data transformation.
  • Class-File API: JDK 23 introduces a preview of an API for processing class files, which aligns with the Java Virtual Machine specification. This API aims to replace the internal ASM library in the JDK and includes refinements such as a streamlined CodeBuilder class for bytecode instructions.
  • Primitive Types in Patterns, instanceof, and switch: This preview feature enhances pattern matching by allowing the use of primitive type patterns in all pattern contexts and extends instanceof and switch to handle all primitive types. This improvement aims to reduce the risk of unsafe casts and provide a more uniform approach to data exploration.

Notable Omissions and Additional Tools

  • String Templates: Initially previewed in JDK 21 and JDK 22, string templates were dropped from JDK 23. The community decided that the feature needed further evaluation and redesign, delaying its inclusion in this release.
  • Java Management Service 9.0: In conjunction with JDK 23, Oracle released Java Management Service (JMS) 9.0, a cloud-native service on Oracle Cloud Infrastructure (OCI). JMS 9.0 offers a unified console and dashboard to manage Java runtimes and applications across on-premises and cloud environments.

Support and Availability

JDK 23 is a non-LTS (Long-Term Support) release, meaning it will receive only six months of support from Oracle, in contrast to JDK 21’s multi-year support. This release continues Java’s evolution, bringing in innovative features while also setting the stage for future changes, such as the deprecation of legacy methods and the introduction of new programming paradigms.