Project Valhalla, Explained: How a Decade of Work Arrives in JDK 28
After a decade of development, Project Valhalla's first phase, bringing "Value Classes and Objects" (JEP 401) to Java, is finally landing as a preview feature in JDK 28. This long-awaited change fundamentally redefines Java's object model by allowing objects to behave like primitives for significant performance gains, addressing a core assumption that has stood since 1995. The community is buzzing with anticipation and debate over this deep technical shift and its implications for high-performance Java.
The Lowdown
Project Valhalla, a monumental decade-long effort to introduce value types into Java, is seeing its initial integration into OpenJDK 28. JEP 401 (Value Classes and Objects), alongside JEP 402 for Enhanced Primitive Boxing, will be available as a preview feature, marking a profound shift in Java's foundational object model.
The core promise of Valhalla is to allow types that "code like a class, works like an int." This means developers can write standard, readable classes that the JVM can treat with the efficiency of primitives, enabling flat and dense memory layouts. This addresses a critical performance bottleneck in modern hardware where the CPU-memory speed gap makes data locality paramount, an issue exacerbated by Java's traditional "fluffy" (scattered) reference types and unpredictable escape analysis optimizations.
Key aspects of this transformative project include:
- Historical Evolution: The concept of value types predates Java's release, but its complexity led to its omission. Project Valhalla officially began in 2014, progressing through several prototypes (e.g., "Q World" and "L World") and evolving naming conventions from "value types" to "inline classes," and briefly, "primitive classes," before settling on the current "value classes."
- Design Breakthroughs: A pivotal realization was that the language model and JVM model do not require a 100% overlap, allowing a simpler programming model with a more complex, optimized JVM implementation underneath. The ambitious dual-projection "primitive classes" model was ultimately simplified to orthogonal concepts of "value classes" and future "null-restricted types."
- JEP 401 in JDK 28 (Preview):
- Declaration: Value classes are declared with the
valuemodifier; they have implicitly final fields, cannot be synchronized upon, and are final by default, unable to inherit from identity classes. - No Identity: The defining trait is the absence of identity. Consequently, the
==operator now checks for substitutability (recursive field comparison) rather than memory address. Attemptingsynchronizedon a value object will throw anIdentityException. - Performance Optimizations: This enables key JVM optimizations like scalarization (breaking objects into their constituent fields) and heap flattening (inlining object data directly into fields or array cells). This leads to significantly denser memory layouts, reducing garbage collection overhead and improving cache efficiency.
- Boxing Overhead Reduction: Primitive wrapper classes like
Integerwill become value classes under preview, drastically diminishing boxing overhead. An array likePoint[](wherePointis a value class) can now store values contiguously, dramatically improving memory access compared to an array of pointers to scattered objects.
- Declaration: Value classes are declared with the
- Future Work: It's crucial to note that JDK 28's preview does not include full specialized generics (meaning
ArrayList<Point>won't immediately see the full flattening benefits), null-restricted types, or 128-bit encodings. These advanced features are planned for future releases.
Valhalla is not merely adding a new feature but fundamentally altering Java's deepest assumption that "every object has identity." While JDK 28 delivers only the first, preview stage, it sets the groundwork for unlocking a decade of further performance enhancements in Java, particularly for data-intensive applications, without sacrificing the language's strong abstraction capabilities. Developers are encouraged to experiment with the preview to provide feedback.
The Gossip
A Nod to .NET Nuances
The discussion often compares Java's new value classes to C# structs, with some commenters noting that .NET has had similar concepts for years. While some view Valhalla as Java merely 'catching up,' others argue that Java's implementation is uniquely challenging due to its stringent backward compatibility requirements and existing ecosystem, making it a genuine breakthrough within its specific context. Differences in mutability and identity handling between C# structs and Java's value classes are also highlighted.
Design Debates and Definitional Difficulties
Commenters expressed concerns and questions regarding specific design choices within Valhalla. Criticisms centered on the decision to defer null-restricted types, arguing that separating null-safety from value types makes the model 'mentally heavier' for developers rather than simpler. The redefinition of `==` for value classes to compare internal state instead of identity also sparked debate, with some arguing it breaks encapsulation and exposes implementation details. Questions were raised about the necessity of 'no identity' for performance and the limitations imposed by atomic write sizes for flattening.
Performance Ponderings and Progress Points
The community keenly debated the actual performance benefits of Valhalla, with some commenters eagerly awaiting benchmarks to quantify the promised speedups. Disappointment was voiced over the delay of fully flattened generic collections like `ArrayList<Point>` to future JDK releases. There was also a historical quibble, with one user challenging the article's claim that CPU and memory access costs were similar in 1995, suggesting it glosses over early performance challenges and Java's development context.
Stewardship Scrutiny and Oracle's Oversight
Some commenters broadened the discussion to critique Java's overall stewardship under Oracle, drawing unfavorable comparisons to the perceived decisiveness in .NET's evolution. Concerns were raised about Oracle's focus, suggesting a shift towards data center and compute businesses might deprioritize Java's development. However, others countered this, asserting Java's continued core relevance to Oracle and its contractual obligations, while acknowledging .NET's own flaws.