What’s Coming in Java 2022? Inside the Valhalla Project’s Value Classes

The article explains the upcoming Java Valhalla project, detailing how value objects, primitive classes, and specialized generics will reshape the JVM’s type system, reduce memory overhead, and introduce new value‑class semantics for more efficient Java programming.

macrozheng
macrozheng
macrozheng
What’s Coming in Java 2022? Inside the Valhalla Project’s Value Classes

Valhalla

Brian Goetz’s "State of Valhalla" article revealed that the Valhalla project, started in 2014, aims to bring more flexible, flat data types to the JVM. In 2021 the project will introduce value objects, primitive classes, and specialized generics.

Value objects are objects without identity; they are compared by value rather than reference.

Shortcomings of Java’s Type System

Java’s built‑in ten primitive types cannot directly express complex structures such as 3‑D coordinates or vectors, forcing developers to model them with classes, which adds memory overhead.

Two objects with identical fields occupy different memory addresses, and primitive values like two ints with the same value have no meaningful identity.

So, in a sense, they have their own identity markers.

Object Header

The object header stores metadata such as lock information, GC flags, hash code, and a type pointer that enables dynamic dispatch, reflection, and inheritance.

On a 64‑bit JVM the header occupies at least 16 bytes (8 bytes on 32‑bit). Many objects carry unnecessary metadata, leading to memory bloat.

Value Class

Valhalla introduces a new class kind called Value Class (currently a JEP draft). Example implementation of a value‑based Substring class is shown below.

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 several characteristics:

Identity is based on == rather than equals().

All fields are implicitly final.

The class does not extend or implement java.lang.IdentityObject; its superclass is either a stateless abstract class or Object.

They implicitly implement java.lang.ValueObject.

No super constructor call is performed, so no superclass initialization code runs. synchronized is prohibited.

They may omit finalize() and avoid using this in constructors.

value – will it become a reserved word or a keyword?

That’s Not All

While Value Classes can reduce Java’s memory footprint by trimming object headers, the Valhalla project also includes many other advanced features beyond the scope of this article.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMMemory OptimizationValue ObjectsValhalla
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.