Value Types & List<int> coming for Java 10 ?

Oracle, with lead engineer Brian Goetz, have launched an experimental OpenJDK project to bring long-awaited features to the Java platform.

Major enhancements to generics & new ‘value types’ are planned. Highlights include:

  • Value Types;  highly-efficient small ‘objects’ without inheritance.
  • Reified Generics;  retaining their actual type at runtime.
  • Generic Specialization;  List<int> would be valid & highly efficient.
  • ‘volatile’ enhancements.

The effort is named Project Valhalla. Here’s a preview:

List<int> — Generic Specialization

Valhalla looks to extend Java generics support beyond Object types to include primitives and value types.

While datatypes such as Integer and Long are currently supported, these ‘boxed’ datatypes require extra allocation & memory overhead.

A particular concern for high-performance processing is the pointer overhead of accessing ‘boxed’ values, which almost always causes a cache miss. This reduces fast loops on int[] to an endless series of cache misses.

Generic specialization would look to resolve this, and further extend Java’s popularity for high-performance & financial processing.

Specialization would need to be implemented at both the compiler and JVM levels. Generic types would have bytecode marked for possible specialization, and ‘reified type’ information would enable the JVM to “specialize” that bytecode.

According to early proposals, the JVM might instantiate an ArrayList<int> by automatically converting the bytecode of ArrayList<T extends Object>. Bytecodes & method signatures with type T would be replaced with their ‘int’ equivalents.

One interesting wrinkle in this proposal, is that List<int> would no longer descend from List<Object>. List<int> would not even descend from it’s raw type, List!

This is due to Java having no ‘Any’ type, able to unify both objects & primitives. As currently proposed, there is no common ancestor.. This is bound to be a subject of discussion.

Value Types

These have long been of interest to enable Java functions to return multiple (tuple) results, and for scientific & graphics calculation efficiency.

Value Types would be memory-efficient, comparable with a C/C++ ‘struct’, and likely to take no more memory than the fields or primitives they are composed of.

Synchronization and inheritance features would be excluded for Value Types. These would no longer have object identity, or the memory & pointer overheads associated.

Removing java.lang.Object’s VMT & synchronization structures will simplify memory layout, eliminating the necessity of an allocation & pointer dereference for each instance.

The proposal suggests Value Types will also able to assume a ‘boxed’ representation,  compatible with Object & passed by reference. This would give interoperability.

Cursors and Iterators are mentioned as a possible applications of Value Types, but one technical hurdle will be to resolve how these can be compatible with the existing java.util.Iterator interface.

Biggest gains from Value Types are expected to be arrays and generic collections, which will be able to be use arrays of the new value types.

When do I get these new features?!!

Project Valhalla is still experimental. Generics, in particular, pose many challenges. None of this will be in Java 9.

This also ties in with people such as Stephen Colebourne (Joda Time)’s push for #BIjava — backwards incompatible Java, to ditch legacy features & move forward.

Generic specialization presents significant change. Method declaration & language enhancements will be needed to solve tricky problems, such as distinguishing  List.remove(int index) from List.remove(Object item).

As the project progresses, classfile formats will change & libraries need to be recompiled.

Essentially, this is a long-term technical effort. Project Valhalla is underway.. we can probably expect results near Java 10.

Are you excited about Project Valhalla? Post your thoughts now.

Read more:

4 thoughts on “Value Types & List<int> coming for Java 10 ?”

    1. While Java seems more popular , and has much richer open-source ecosystems & innovation around it, it’s true that C# is a good language & has some great features.

      LINQ is my favorite, but ‘typed generics’ are a good technical feature. However, Java’s implementation of ‘typed generics’ will be able to benefit by learning from some of C#’s mistakes in this area; eg. static fields.

      Enjoy the platform the work with & don’t feel the need to cast shade. Maybe even consider opening your horizons?

  1. I would like to see some other features. I would like to see typedef like in C:

    typedef StringToString Map
    StringToString foo;

    With generics, we have to type a lot the same thing again and again. With typedef it would also give a name and more meaning.

    I would also like to see enums like in Swift or Rust where the different values of the members can also have different types. That would be great for JSON. It such a pain to work JSON in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *