Project Valhalla: Codes Like a Class, Works Like an int
What is Project Valhalla?
When Java applications create many objects, performance suffers significantly. Each object requires memory allocation on the heap, carries overhead from object headers, and forces the garbage collector to work harder. This becomes especially problematic in data-intensive applications where simple data structures like coordinates, colors, or mathematical values are wrapped in heavyweight objects. Project Valhalla is Oracle’s ambitious initiative to bring value types to the Java platform. It aims to provide “codes like a class, works like an int” semantics. Value types allow developers to create custom data types that behave like primitives in terms of memory layout and performance while maintaining the expressiveness of classes.
The Core Problems Valhalla Solves
Traditional Java forces a trade-off between abstraction and performance. You can either use primitives for speed or objects for abstraction, but not both. A simple Point class with x and y coordinates requires two object allocations plus overhead, consuming far more memory than necessary. Generic collections suffer particularly from this limitation. An ArrayList of Integers must box each primitive int into an Integer object. This creates millions of unnecessary objects and pointer indirections that slow down processing.
Value Classes: The Solution
Value classes look like regular classes but behave fundamentally differently. They have no identity, cannot be null, and are immutable by default. The JVM can store them inline without object headers or heap allocation.
public value class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int x() {
return x;
}
public int y() {
return y;
}
}
This Point value class consumes only 8 bytes of memory instead of the 24+ bytes required by a traditional object.
Generic Specialization
Valhalla also introduces generic specialization. Current Java generics use type erasure, forcing all generic types to work with Object references. This prevents efficient storage of primitives in collections. Specialized generics allow collections to work directly with value types and primitives. An ArrayList of Points can store the Point data inline without boxing or pointer indirection.
Performance Benefits
Early benchmarks show dramatic improvements. Arrays of value types can be 2-4x faster to process than arrays of objects. Memory usage drops by 50-75% in many cases. Cache performance improves significantly because related data stays together in memory. Garbage collection pressure decreases substantially. Fewer objects mean fewer allocations and less work for the garbage collector. Applications spend more time doing useful work instead of managing memory.
Current Status and Timeline
Project Valhalla remains in active development. Preview builds are available for experimentation, but the feature set continues to evolve. The full integration of Valhalla’s features, including value classes, is expected to take place over several releases, with the first preview potentially appearing in JDK 23.
Conclusion
Project Valhalla will significantly improve Java performance. Applications will use less memory and run faster. This is excellent news for developers building high-performance systems. However, Valhalla makes Java more complex. Java currently has objects and primitives. Valhalla adds three new types: value classes, primitive classes, and (heaven help us) identity classes. New developers must now learn five different type categories instead of two. This creates challenges for beginners. They need to understand when to use each type and how they behave differently. The simple “everything is an object” mental model no longer applies.
Java adds new features to stay competitive, but each addition makes the language harder to learn. This creates a paradox: the very changes meant to keep Java relevant may drive away new developers.
Sources: