{{announcement.body}}
{{announcement.title}}

L-World Value Types and EA Builds in Project Valhalla

DZone 's Guide to

L-World Value Types and EA Builds in Project Valhalla

Check out this quick tutorial on how to play around with the new value types in Project Valhalla's early-access builds and IntelliJ. Click here to learn more!

· Java Zone ·
Free Resource

A few days ago, Project Valhalla released an early-access build. I started to play around with it using command line tools, but when I tried to configure IntelliJ, I discovered that is much easier than I anticipated. So, I’m sharing the instructions here, in case they may be useful to someone else.

First of all, what is Project Valhalla and the L-World? This is probably one of the major changes coming to Java in the near future. This is a change as big as lambdas or the module system: the introduction of value types.

This is the best description I found on what they are:

Value types are intended to be a third form of data type available in some future version of Java to complement the currently-existing two: primitive types and object references. The phrase most often used is that Java value types should “code like a class, work like an int." This means that they should be a composite data type (code like a class) but lack identity and not pay the object header cost, if at all possible (work like an int).
- Ben Evans ( @kittylyst)

There are a lot of resources on the Internet about it, and it is very exciting to be able to follow the project from the first discussions to the current day-to-day implementation details on mailing lists and talks.

But, since we are often bored by long explanations, let’s get into it!

First, you have to visit the official site and download the J11-modified JDK.

EA builds download page

Click on your architecture and download the tar.gz. I did the test on a Linux box with Ubuntu 16.04, as a reference.

Once you download the tar.gz file, you have to extract on the same directory with the other JDKs. It came with the nondescript namejdk-11, so I renamed it.

I renamed the jdk-11 as jdk-lworld-20180801

Once you have done this, you have already completed the most complex part. Now, you have to open IntelliJ and create a new Java command line project. There is no need to setup JAVA_HOME or any other env vars.

On the project settings, press the new SDK button and select the directory that you have just extracted. It should come as “11” in the ComboBox.

Then, you select the X-Experimental features as Project language level.

Image title

Now, you can create your value types and use them for your project!

If you want, you can start copying my code here.

This is the main:

Image title

And, this is the Point value type:

Image title

Although the editor shows the (temporary) new syntax in red, it will compile and run without problems.

When declaring a value type, you to specify the  __ByValue modifier before the class name.

Note that to create a new value type instance, you shouldn’t use the keyword new but  __MakeDefault and __WithField to return a modified instance.

To run the code, you have to remember to specify the -XX:+EnableValhalla VM option in this way:

Image title

Other possible options are listed here.

Now, you can start experimenting yourself! It is especially interesting to compare the memory allocation performance and how arrays work. In the future, there will also be reified generics for value types.

So far, there are two things that I’m surprised about: == doesn’t work on value types (I suppose it will eventually), and new is actually working (I suppose it won’t?).

In any case, it is really great to be able to play around with these new additions! 

Topics:
java ,value types ,intellij ,java 11 ,project valhalla ,jdk 11

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}