Project Valhalla (Java language)
| Company type | OpenJDK community project |
|---|---|
| Industry | Software development, Programming languages |
| Founded | July 2014 |
| Founder | Brian Goetz |
Area served | Java programming language |
| Products | Proposed Java language features (Value Classes, Primitive Classes, Reified Generics) |
| Website | openjdk |
Project Valhalla represents an experimental effort within the OpenJDK community aimed at introducing significant new language capabilities for Java 10 and subsequent versions. Initiated in July 2014, this project is spearheaded by Oracle engineer Brian Goetz and concentrates on boosting Java's efficiency and expressive power.
Anticipated Enhancements
Valhalla serves as a testing ground for several critical improvements in Java, including:
- Value Classes and Objects: streamlined objects that lack identity (reference values).
- Nullability constraints and Nullable type annotations: new syntax to define null acceptance, such as
?or!suffixes on types. - Advanced Primitive Boxing: support for constructs like
List<int>. - Reified Generics: maintaining concrete type information during runtime.
Implementing these features necessitates modifications both in the language syntax and the JVM infrastructure.
Project Structure
The initiative is divided into multiple JDK Enhancement Proposals (JEPs):
- JEP draft: Value Objects (Withdrawn)
- JEP 401: Primitive Classes (Preview)
- JEP 402: Enhanced Primitive Boxing (Preview)
- JEP draft: Universal Generics (Withdrawn)
- JEP draft: Null-Restricted Value Class Types (Preview)
Value Classes
Value classes function as reference types without inherent identity; equality comparisons rely on the equivalence of their components rather than object identity. These classes prohibit synchronization on their instances.
Although value classes support , the JVM frequently optimizes by avoiding heap allocation for them. Nevertheless, when stored in object fields or cast to interfaces, heap allocation remains necessary.
Several existing Java API types, often referred to as value-based classes, are prospective candidates for transformation into value classes in future Java versions.
Primitive Classes
Primitive classes inherit all restrictions of value classes but differ by not being reference types, thus lacking support. Their default initialization values correspond to zero equivalents of their components (e.g., 0 for numeric types, false for booleans, null for references, and zero for nested primitives).
These classes are always stored inline, completely avoiding heap allocation. Arrays composed of primitive classes eliminate pointer indirection to heap memory. When required, conversions will box primitive classes into value class representations and unbox back accordingly.
Fundamental Primitive Classes
This proposal seeks to represent the traditional JVM primitive data types—byte, char, short, int, long, boolean, float, double—as primitive classes. This approach reduces the need for special handling of these eight fundamental types within the type system.
Technical Advantages and Consequences
A primary objective is to improve memory access speeds and enhance efficiency when handling boxed values. Features such as Value Types and specialized Generics reduce memory overhead and prevent pointer indirections that typically cause cache misses.
Rather than storing references scattered throughout memory in arrays or collections, Project Valhalla facilitates contiguous memory layouts of values without intermediate pointers.
Value Types are designed to "program like a class but perform like an int," omitting synchronization and inheritance to eliminate identity-related overhead while still supporting a boxed variant for backward compatibility.