{{Short description|Java language upcoming features}}
In computing, '''Project Valhalla''' is an experimental OpenJDK project to develop major new language features for Java 10 and beyond. The project was announced in July 2014 and is an experimental effort by Oracle, led by engineer Brian Goetz.<ref>{{cite web|last1=Goetz|first1=Brian|title=Welcome to Valhalla!|url=http://mail.openjdk.java.net/pipermail/valhalla-dev/2014-July/000000.html|website=OpenJDK mail archive|publisher=OpenJDK|accessdate=12 August 2014}}</ref>
The project page<ref name="valh">{{Cite web | title=Project Valhalla | url=https://openjdk.org/projects/valhalla/ | access-date=2026-02-28 | website=openjdk.org}}</ref> offers an overview as well as links to technical drafts and early-access builds.
==Planned features==
Valhalla is incubating Java language features and enhancements in these areas:<ref>{{cite web|last1=Evans|first1=Ben|title=Oracle Launches Project Valhalla for Java|url=http://www.infoq.com/news/2014/07/Project-Valhalla|website=InfoQ|publisher=InfoWorld|accessdate=12 August 2014}}</ref><ref name="valh"/> * Value Classes and Objects: highly-efficient objects without their own identity (reference value). * Null-restricted and Nullable types, and Null-restricted Objects: for example, using <code>?</code> or <code>!</code> after type declaration to say if null is allowed or not. * Enhanced Primitive Boxing: for example, to allow code such as <code>List<int></code>. * Reified Generics: retaining actual type at runtime.
These features will require both syntax and VM-level changes.
==Project components== The project is organized into several JEPs (JDK Enhancement Proposals): * JEP draft: Value Objects (Withdrawn)<ref>{{Cite web | title=JEP draft: Value Objects (Preview) | url=https://openjdk.org/jeps/8277163 | archive-url=https://web.archive.org/web/20220630163354/https://openjdk.org/jeps/8277163 | access-date=2026-02-28 | archive-date=2022-06-30}}</ref> * JEP 401: Value Classes and Objects (Preview)<ref>{{Cite web | title=JEP 401: Value Classes and Objects (Preview) | url=https://openjdk.org/jeps/401 | access-date=2026-02-28 | website=openjdk.org}}</ref> * JEP 402: Enhanced Primitive Boxing (Preview)<ref>{{Cite web | title=JEP 402: Enhanced Primitive Boxing (Preview) | url=https://openjdk.org/jeps/402 | access-date=2026-02-28 | website=openjdk.org}}</ref> * JEP draft: Universal Generics (Withdrawn)<ref>{{Cite web | title=JEP draft: Universal Generics (Preview) | url=https://openjdk.org/jeps/8261529 | access-date=2026-02-28 | website=openjdk.org}}</ref> * JEP draft: Null-Restricted Value Class Types (Preview)<ref>{{Cite web | title=JEP draft: Value Object Storage Enhancements (Preview) | url=https://openjdk.org/jeps/8316779 | archive-url=https://web.archive.org/web/20230925160139/https://openjdk.org/jeps/8316779 | access-date=2026-02-28 | archive-date=2023-09-25}}</ref>
=== Value classes === Value classes are reference types, in the same way as all existing Java classes. However, they give up the ability to have identity. This means that the {{java|<nowiki>==</nowiki>}} operator compares instance of the value class by equality of their components, instead of by identity. Additionally, synchronizing on instances of value classes will fail.
Value classes still support {{java|null}}, since they are reference types. The Java Virtual Machine is expected to be able to take advantage of the additional constraints of value classes to eliminate heap allocation of value types in the vast majority of cases. However, storing instances of value classes into a field or upcasting them to an interface will still require an allocation.
Existing types in the Java API such as {{java|java.util.Optional}} are known as value-based classes, and are candidates for being made into value classes in a future JDK release.
=== Primitive classes === Primitive classes are subject to all the constraints of value classes, but are not reference types. This means they give up the ability to support {{java|null}}. Instead, their default values are the zero value for each of the component types ({{java|0}} for numerical types, {{java|false}} for booleans, {{java|null}} for reference types, and the zero value for nested primitive classes).
All primitive classes are stored "inline", that is, without requiring a heap allocation. Arrays of primitive classes will not require a pointer indirection from the array to the heap. Where needed, conversions will be inserted to "box" the primitive class into a value class version of itself and vice versa.
<syntaxhighlight lang="java"> import java.util.ArrayList; import java.util.List;
// List<int> instead of List<Integer> List<int> intList = new ArrayList<>();
intList.add(10); intList.add(20);
System.out.printf("Integer List: %s%n", intList);
// List<double> instead of List<Double> List<double> doubleList = new ArrayList<>(); doubleList.add(3.14); doubleList.add(2.718);
System.out.printf("Double List: %s%n", doubleList); </syntaxhighlight>
=== Classes for the basic primitives === This JEP is meant to express the classical primitive types of the Java Virtual Machine (<code>byte</code>, <code>char</code>, <code>short</code>, <code>int</code>, <code>long</code>, <code>boolean</code>, <code>float</code>, <code>double</code>) as primitive classes.
Traditionally, the eight primitive types are treated separately from all other types. Providing primitive class declarations from them removes much of this special-casing, leading to a more elegant and easy to understand type system.
==Technical benefits and implications==
Memory access performance and the efficiency of 'boxed' value access are a major area to be addressed by these features. 'Value Type' features and 'Generic specialization' (when applied to lists or collections) reduce memory usage, but more importantly avoid pointer indirection which typically causes a cache miss.<ref>{{cite web|last1=Krill|first1=Paul|title=Next generation Project Valhalla proposed; Value types, generic specialization, and enhanced volatiles top the wish-list for Project Valhalla|url=http://www.javaworld.com/article/2385002/core-java/java-incubator-to-explore-technologies-for-java-10-and-beyond.html|website=JavaWorld|publisher=InfoWorld|accessdate=12 August 2014}}</ref><ref name='LiterateJava'>{{cite web|title=Value Types & List<int> coming for Java 10 ?|url=http://literatejava.com/java-language/value-types-list-int-coming-java-10/|website=LiterateJava.com|accessdate=12 August 2014}}</ref>
Instead of a list or array of object references, pointing to data values scattered throughout memory, Project Valhalla enhancements will enable list or array values to potentially be laid out linearly—without indirection—as a consecutive block of memory.
Value Types are envisaged as "Codes like a class, works like an int!"<ref name='Value Types proposal'>{{cite web|last1=Rose|first1=John|last2=Goetz|first2=Brian|last3=Steele|first3=Guy|title=State of the Values|url=http://cr.openjdk.java.net/~jrose/values/values-0.html|publisher=OpenJDK|accessdate=12 August 2014}}</ref> Synchronization and inheritance would be excluded for Value Types. These would no longer require object identity and associated memory/ pointer overheads, though would be able to assume a 'boxed' form for compatibility.<ref name='LiterateJava'/>
Brian Goetz, the engineer behind Project Valhalla, suggested that operator overloading could be added to Java after the introduction of value types.<ref>{{Cite tweet |user=BrianGoetz|number=819272509535219712|title=Value types first, then we can talk about it.}}</ref>
==See also== * Generics in Java * Generic programming * Value type
==References== <references/>
== External links == * [http://www.javaworld.com/article/2385002/core-java/java-incubator-to-explore-technologies-for-java-10-and-beyond.html Java incubator to explore technologies for Java 10 and beyond - JavaWorld] * [http://literatejava.com/java-language/value-types-list-int-coming-java-10/ Value Types & List<int> coming for Java 10? - LiterateJava.com] * [http://openjdk.java.net/projects/valhalla/ OpenJDK - Project Valhalla]
Category:Java (programming language)