Java 14 – “It feels like the early days of Java.”
For the release of Java 14 we talked to Java expert Dr. Heinz Kabutz. What does he like and dislike about Java 14? What is he currently working on? What does he still find disturbing in 2020? Read on to find out…
JAXenter: What do you think is the most important new feature in Java 14?
Dr. Heinz Kabutz: A few months ago, Brian Goetz complained on the Java Champion mailing list that they were not getting enough feedback on their preview features. As a result, I decided to from now on always run my JavaSpecialists.eu website on the latest preview version of Java. If you look at the website right now, you will see “Running on Java 14-ea+29-1384 (Preview)” at the top. It is a bit of work to do this. Besides my master branch, I have three other branches for the Java 14 features: java14-preview, jep359 (records) and jep368 (text blocks). I generally work in master, but then rebase the three other branches off that.
You might be tempted to think that JavaSpecialists.eu is just a hobby website and that it would not carry much risk if that was down. Not so. It is my bread and butter. It is the primary reason why you are reading this now. Without it, you would never have heard of Heinz Kabutz. I absolutely cannot afford to have it go down. I thus have constant monitoring to verify that everything still works.
Of all the features that are in Java 14, I like the records (JEP 359) the most.
However, an interesting new addition is that ConditionNode inside the AbstractQueuedSynchronizer is now also a ManagedBlocker. This means that ReentrantLock’s Condition plays nicely with the ForkJoinPool, and thus parallel streams. LinkedBlockingQueue is based on ReentrantLock, and so that also cooperates well. I wrote about the possibilities, proposing a hack to fix LinkedBlockingQueue. With Java 14 we get it working correctly out of the box.
JAXenter: What feature do you think is missing in Java 14?
Dr. Heinz Kabutz: So I’m busy writing a book on dynamic proxies in Java. I wrote a German version many years ago, but this is a complete rewrite. The thing that has wasted most time was the Java Platform Module System (JPMS). We spent days trying to get Maven, IntelliJ and JPMS to work together. We eventually managed with the help of fellow Java Champions Simone Bordet and Robert Scholte. It is the year 2020 and Java 9 features are STILL not working properly in the tool chain.
JAXenter: With features like “JEP 358: Helpful NullPointerExceptions“, is Java approaching “modern” languages like Kotlin?
I enjoy looking up the new features and trying them out. It feels a bit like the early days of Java, when we saw big gains happen quite regularly.
Dr. Heinz Kabutz: “Modern” is good. The challenge with a language like Kotlin is that they need to also support older JVMs. Quite a few nice new Java performance enhancements are not available yet on Kotlin. For example, String has been overhauled to allow us to have fast appending with +. However, the bytecode that Kotlin generates is still that of Java 8.
I do like how Kotlin avoids the dreaded NullPointerException. Unfortunately the “Helpful NullPointerExceptions” are quite a far cry from this. Instead, they simply show a bit more information about what was null, rather than avoiding the possibility of NullPointerException altogether.
SEE ALSO: Java 14 News Thread
JAXenter: What wishes/preferences do you have for Java 15, set for release later this year?
Dr. Heinz Kabutz: That they finalize records as soon as possible. The whole concept seems well thought out, even the way that serialization works is clever.
JAXenter: In general, what do you think about the acceleration of Java with the six month release cycle?
It is the year 2020 and Java 9 features are STILL not working properly in the tool chain.
Dr. Heinz Kabutz: It is a bit chaotic and hard to remember what features belong to which version. Most companies are still on Java 8. Those that have moved forward are using Java 11. Only very few are brave (or stupid) enough to run on the latest bleeding edge JVM.
That said, I am enjoying looking up the new features and trying them out. It feels a bit like the early days of Java, when we saw big gains happen quite regularly.
JAXenter: Your Extreme Java Camp is about the use of Java 8 streams and lambdas, among other things – are there any best practices you can share with our readers?
Dr. Heinz Kabutz: Streams can help make code a lot simpler to understand, if used correctly. In our workshop we refactor classes to use streams. After spending a day doing this on “throwaway code”, we get a very good idea of where and when to use streams.
SEE ALSO: How To Securely Program in Java in 2020
Here’s a top tip for those not using lambdas all the time: Write code using anonymous inner classes and then use the refactoring in your IDE to semi-automatically convert it to lambdas. After we’ve done this 100 times, it becomes easy and we can write the lambdas directly.
JAXenter: You also teach people about Java threads. Why are they so important?
Dr. Heinz Kabutz: Every piece of Java code that has ever been run, has executed in a thread. We absolutely need to understand what threads are and how to use them, so that we can avoid the pitfalls that can hurt us in our projects. For example, yesterday I was writing an exercise for a new course that I am creating on Dynamic Proxies in Java. Whilst running it, we discovered that it sometimes gave an unexpected NullPointerException. It did not happen all the time. This seemed very much like a race condition. However, it also happened when running it with a single thread. Using the techniques in our course, I quickly narrowed it down. It was indeed a race condition, but between the JVM garbage collector threads and my application. Adding a lock would not have helped. Understanding what to look for and how to diagnose the issues saved many hours of frustration.
Thanks very much!