Java, Today and Tomorrow

Thursday, November 22, 2018

Java is an ever evolving object oriented language. Devoxx was the ideal moment to refresh what we can expect of Java in the future. At this moment, Java has 4 main projects going on. Let’s take a look at how they are called and what they do.

This post is a brief recap of a session given by Mark Reinhold at Devoxx 2018. The complete presentation can be found online at https://www.youtube.com/watch?v=Csc2JRs6470.

1. Project Amber: Right-sizing language ceremony

“The goal of Project Amber is to explore and incubate smaller, productivity-oriented Java language features.” This is stated on the project’s website. This means that we still want to produce good readable code, but with less lines or characters.

First example is the Local-Variable Type Inference.

Take this line: 

BufferedReader reader = new BufferedReader(new FileReader(file));

The info in this line is actually duplicated. On the left we read BufferedReader and on the right we read the same.
Do we need it on the left side or is this code still readable when it’s not there?

With the var it becomes:

var reader = new BufferedReader(new FileReader(file));

It still contains all the info needed to understand this line of code.
Are you now adding var on every line of code? I really hope not, use it when it actually helps you make your code more readable, without losing any information of the line.
Good code is self-explaining, we need to keep that.

In JDK 12 they will also add Raw String Literals.
How fun can it be that we test some regex online, then we copy it to our IDE and we don’t need to escape any character. Some IDE do add the escaping directly in it, but still. We need to check if it’s there, in some rare occasions we need double escaping and it start looking weird.

Take this line of code: 

System.out.println(”this”.matches(“\\w\\w\\w\\w”));

When JEP 326 is in the JDK, we could just use: (it’s a back tick, not single quote)

System.out.println(”this”.matches(`\w\w\w\w`));

But the fun doesn’t end there, think of path to directories,... Everything you need to escape can be solved with the back tick.

Check out all the changes on the projects website.

2. Project loom: continuations & fibers

“The goal of this Project is to explore and incubate Java VM features and API's built on top of them for the implementation of lightweight user-mode threads (fibers), delimited continuations (of some form), and related features, such as explicit tail-call.” according to the project's website.

Mark Reinhold demonstrated during his talk that Threads are pretty heavy weighed. Fiber, on the contrary, is a light weight thread and Reinhold was able to demonstrate the amazing performance difference between Thread and Fiber in Jetty. 

3. Project Panama: foreign-function/data interface

“We are improving and enriching the connections between the Java TM virtual machine and well-defined but “foreign” (non-Java) API's, including many interfaces commonly used by C programmers.” according to the project's website.

For most developers this project is quite far-fetched, we rarely need to integrate different API’s to our virtual machine, but for those who actually do need it, will it make your life less complicated.

4. Project Valhalla: value types & specialised generics 

“The goal of this Project is to provide a venue to explore and incubate advanced Java VM and Language feature candidates such as:

What does this means.?
Java is from the 90’s.

Think about the computers in the 90's. I'm sure the one you owned at home is bigger en slower than your current smartphone. 
Not only the size has changed, the processors changed too.
Every Object in Java has underlying pointers, which makes it slower.
On the other side, big data is coming up(/here to stay). We need to process a lot of data, faster than ever.
But, we can’t break our existing code.
The solution is pure data aggregators, who don’t have any pointers.
With the value keyword in your class :

public value class Complex {

We could make this object a real data class with no pointers in the back.
Mark's test increased the instructions per cycle from 1 instruction to 2.75 instructions.
The allocation rate is dropping from a heavy 981MB/sec to 13Mb/sec.

This project is definitely worth looking at if you have a lot of raw data that needs to be processed. Even if you don’t use a lot of data, lighter and faster code is always a bonus.

5. Summary

Each project has it's own potential.
My personal favorite is project Amber.
I’m a big fan of writing less code while it’s still easy to read and understand.