What’s New in Java 2022? Inside the Valhalla Project’s Value Objects
This article explains the Java Valhalla project, its upcoming value objects, primitive classes, and specialized generics, discusses the limitations of Java's type system and object headers, and introduces the concept and features of value classes with code examples.
Valhalla
Brian Goetz’s recent "State of Valhalla" article reveals that the Java project team launched the Valhalla project in 2014 to bring more flexible, flat data types to the JVM. In 2021 the project plans to introduce value objects, primitive classes, and specialized generics.
Value objects are objects whose identity is irrelevant; they are compared by their value rather than by reference.
Shortcomings of Java’s Type System
Java’s built‑in ten primitive types cannot directly represent complex structures such as strings, 3‑D coordinates, or vectors, forcing developers to model business entities with these limited types. Moreover, two distinct objects with identical fields have different memory addresses, making identity‑based comparison meaningless for many use cases.
For primitive types, comparing two variables that both hold the value 7 is obviously meaningless, as they represent the same value rather than distinct objects.
“From this pain point the Valhalla project was born.”
Object Header
Understanding how the JVM stores objects in memory clarifies the benefits of Valhalla’s value objects. An object header contains metadata such as thread access flags, GC marks, hash code, and a type pointer used for dynamic dispatch, reflection, and inheritance. On a 64‑bit JVM the header occupies at least 16 bytes (8 bytes on 32‑bit), which can be wasteful for objects that only need to store value data.
Value Class
Valhalla introduces a new class type called a Value Class, currently a JEP draft. Example:
value class Substring implements CharSequence {
private String str;
private int start;
private int end;
public Substring(String str, int start, int end) {
checkBounds(start, end, str.length());
this.str = str;
this.start = start;
this.end = end;
}
public int length() { return end - start; }
public char charAt(int i) { checkBounds(0, i, length()); return str.charAt(start + i); }
public Substring subSequence(int s, int e) { checkBounds(s, e, length()); return new Substring(str, start + s, start + e); }
public String toString() { return str.substring(start, end); }
private static void checkBounds(int start, int end, int length) {
if (start < 0 || end < start || length < end) throw new IndexOutOfBoundsException();
}
}Value classes share many characteristics with regular classes but have distinct properties:
They have no identity; the == operator and equals() behave the same.
All fields are implicitly final.
They do not directly or indirectly implement java.lang.IdentityObject; their superclass is either a stateless abstract class or Object without state.
They implicitly implement java.lang.ValueObject.
Instance creation does not invoke a super constructor, avoiding superclass initialization.
The synchronized keyword is prohibited.
They may omit a finalize() method.
Constructors may avoid using this to set fields, or may set fields only after all memory is allocated.
While existing JDK classes could be reclassified as Value Classes, compatibility adjustments would be required.
Will value become a reserved word or a keyword?
That’s Not All
Value Classes reduce the size of Java object headers, lowering memory consumption, but this is only a part of the broader Valhalla initiative.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
