Java 9 came out on the 21st September.
There are a huuuge amount of blogs, microblogs and whatnot associated with the new release. I didnt want to re-hash them all here but list what were the most exciting things.
For me, Java 9 is like Java 7 – there is a lot going on under the hood compared to front and center Java, the language, improvements. The most prominent feature is modularity but behind the scenes are some really interesting things worth highlighting
Under the Hood
In this release we have the beginnings of JVMCI, Graal and Truffle, which lay the foundation for Project Metropolis, a way to write the Java Compiler in Java. Aside from that Graal, already has some exciting support for most languages on the JVM and some exciting Node support. There is an ahead of time compiler which allows you to compile your Java code into native libraries (.so files in Linux, and does many of the pre-optimisations that a C1/C2 compiler would normally provide)
Compact Strings are a way to reduce the amount of bytes used to store Strings in Java (no unicode bytes wasted for those where unicode isnt required)
G1 is the default garbage collector, a means of splitting the heap into thousands of separate regions and concurrently keeping up GC’ing without the aim to achieve high throughput.
sjavac is a tool to allow for incremental compilation of Java files. Some IDEs and build tools feature equivalent features, but as sjavac is used in OpenJDK it should help maintainers at least, as well as provide another option to toolmakers.
The Fun Things
JShell finally gives Java a REPL. As far as REPL’s go it is fun to use. It has pretty good tab completion, popping up JavaDocs, helping with imports and variable assignments. You can load in Jars into the classpath or reset the shell session with a new Module Path – (anyone want to try the incubating http2 client feature?, you can with/reset --add-module jdk.incubating.http2client
). It also lets developers leave semicolons behind in single expression statements.
The collection interfaces get the Guava inspired of method, to allow creation of Immutable Sets, Lists and Maps. There is apparently less garbage created than using the old Arrays.asList() from older Java’s.
Reactive Streams (RxJava and friends from the Reactive Manifesto) are now included in the java.util.concurrent.flow package.
jlink lets developers build smaller Java runtime distributions that only have the libraries and modules from the JDK that we use. It goes hand in hand with Modularity, but it enables us to produce smaller runtimes – great for docker images and IoT devices, and naturally will allow for slightly faster startup times.
More frequent releases of OpenJDK and new licensing. OpenJDK releases will now come out every 6 months, with features that are ready, making the train, without the need to hold up a Java release for 4 years (Ahem, Jigsaw) In addition its worth noting that there are only 2 differences now between regular OpenJDK and Oracle JDK – the font rendering and the colour matching are proprietary in the later. Everything else is directly the same. Additionally Oracle will release a LTS release, similar to Ubuntu, every 3 years, for companies that require the safety of a more proven release cycle.
Nashorn Javascript engine supports ES6 syntax, like let, const and arrow functions but unfortunately doesnt support generators. Hopefully this will be coming in a sooner rather than later Java release.
Jigsaw aka why we had to wait 4 years for the next version of Java
Take a 20 year old project, and break it up into smaller chunks. You’ll release there are some long bits of spahgetti. Stuff that probably didnt need to be there. Corba! Oracle spent some time defining a module system that let them modularise the JDK so that we could get some benefits of things like jlink mentioned above.
I’m going to use my 9 seconds really fast, and if you want a more low down on modules you should read State of the Module System by Mark Reinhold however here is the express version.
In Java you have packages as a way to split up classes. Java 9 adds a new concept around a group of packages, called a module, that groups the packages together, whilst defining some encapsulation. You say which packages are available to be used by other modules. Anything you dont declare cant be used by another package and is internal.
Apart from being awesome at encapsulation, the other benefit is that modules now at compile, and run time whether or not code is available to be executed. You can compare this to the old classpath, which would have to scan every single Jar and look for a class file, not knowing if a class would be present or not. To make matters worse, if multiple versions of the same classes appeared on the classpath in different jars, you’d find the infamous jar hell which would lead to method missing and class cast exceptions.
Modules work at the source level. Unlike using a build tool to define what packages in a jar are in or out, there is a new java file called module-info.java that sits at the top level of a src directory and declares what packages each module exports.
module-info.java
module com.first.module { requires java.base; exports com.first.mypackage to com.second.module }
Smaller Changes
You can now search JavaDocs with a search field in the top right.
Try with Resources has been enhanced not to redefine final variables outside of scope within the try (parens) at the top.
Streams have dropWhile, and takeWhile. Optional has some nice methods, in particular a long awaited ifPresentOrElse. They can also be turned into streams
We have more fine grained deprication annotations advising of shelf life of annotations. This will get more useful in later versions of Java
Multi-release Jar files let you build jars with jars that have class implementations for different versions of Java. Their usefulness I’ve heard isn’t super useful just yet since you can only target versions of Java from 9 onwards, and refer to a ‘up to 8’ implementation
Reduction on the use of sun.misc.Unsafe by providing alternates in the jdk itself (Base64Encoder). VariableHandles provides a way to access fields without the reflection tricks of sun.misc.unsafe too.
The JRockit based monitoring tool, mission control, is now included without the need for a special license.
Docker awareness – the JVM can tell the difference between OS memory, and docker container memory, and cpu cores.
What didnt make it in?
As mentioned earlier, there was a new http2 client but it didnt make it in. not sure why, it did seem that there were bug reports in the Java bug db late in the game that compromised the quality of the interface.
Money and Currency API didnt make it. Units of Measurement IIRC hadn’t made the cut a while ago.
So where to now?
In the short term, go and try out Java 9 with your apps. Easiest thing to do is to run them on the command line unchanged. Work out any kinks that come from deprecations and changes to sun.misc.unsafe first.
Build tools will get up to speed soon with being able to produce & consume modules easily. Reuse the right modules in the right paths.
Once you can run with Java 9, start using the JDK to build new byte code in the new classfile and module formats.
Apart from the Java 9 release, there is a still a lot of great stuff in the pipeline. from Oracle – Project Amber and Valhalla, ValueTypes and GPU integrations. Graal will get better, along with ahead of time compilation. Metropolis is leading us to the benefits of building a cross platform language with a cross platform compiler.
Jigsaw IMO is only the start to modularity. Oracle will get the guts to tackle versioning, and I think that will make the community hungry for features in the long standing module systems on the JVM (OSGi) and potentially give the alternate module systems a platform to win over new users.
The JDK can now iterate faster thanks to modularity. Incubation modules is actually a feature in this release. As a result, be prepared to try out new things in Java sooner.
Other community projects have embraced the platform. Spring 5 GA was released as well, already taking advantage of Java 9 features, JUnit 5 has reached a mature release milestone, a new imagining of FindBugs called SureBugs that is refreshingly JDK9 only came out recently too.
And aside from that, it looks like Java EE is getting some momentum, by Oracle simply realising it wasnt the best steward for EE and giving it to the Eclipse foundation.
This has been more than 9 seconds, but the Java world is still on the move, still surprising everyone, and still not just worth 9 seconds of your time. Java is cross platform, but also getting the benefits of some static platform compiled languages. The community is strong, and I look forward to the next 9 seconds, 9 days, and 9 months, to which the Java world will change yet again. Dramatically as always.