Java 9 In Real World
Why do we need Java 9 in Real word. What it offers that you should look into and move your application to production without any second thought, here I have word of caution for you. You would surprise to know that no one is using Java 9 in production. Obviously, it is yet to be released officially by Oracle and tentatively planned in Sep 2017. However you must think and decide why should I not use Java 9 (Jave Development Kit - JDK 9). Even if you don’t do anything, it is free lunch for your application and technology stack and here the list of offering which JDK 9 is bringing to you
- Better User of Memory
- Better Performance of
- Secure Apps
- Better User of Hardwrae
- Better Documentation
- Prettier Graphics (Windows & Linux)
- Faster Compilation
Without using any new feature listed later, you gets above improvement automatically if you run your application using Java 9. Now I hope you have already got your answer for your question “Java 9 in Real World”. Every other instance running in production should immediately plan to move to Java 9 and all above benefit will come as free lunch. Right now (on the time of writing this article) Java 9 is in EA (Early Access) release 168 build.
Is Java 9 all about Jigsaw?
Many techy are talking that Java 9 is all about Jigsaw and this is the elepahant which we need to look for, but if you see the the full list of features which Java 9 is packed with, you would be disappointed with your half knowledge that it is much more than this big elephant. Let me list out some of key feature further in this article at very high level.
REPL (Also known as JShell)
If you are pythong or Ruby on Rail programmer, you must be aware of such REPL which helps you to write small snippet of code to give you inside how things work. Java always had lot of boiler plat items which you have to learn and setup before you even start with small 1+1 calculation or small sysout. Now you have the same power of writing small piece of logic without opening any IDE or editor and test your commands quickly user REPL. Find the tutorial here from java.net
Other features like Module etc
Java 9 new feature list stretches close to 300 and many of them are not for developers and which are part of other productivity improvement parts. This would be hard to list all of them but top 10 key features are listed below
- Developer Convenience
- JVM options
- Native Platform
Compiling with Java 9
You might have heard that if you move your code to Java 9, everything would break. Not really but obviously if you are migrating a multi module project from Java 8 or previous to Java 9, you may see some hiccups. When you are arrange same package name under different module, you need to follow a different strategy to achieve that. When you start using modules in Java 9, your dependencies and other thing may break and you have to be very careful with that. Some of JavaFx may also need some tweaking in EA (Early Access), however Java 9 final release will fix it. The pain which you would have it is primarily on Grade/Maven module arrangement, IDE support might be another area at this stage (Jigsaw, auto complete etc), reorganisation of file might be a trivial item
Following items may not work in Java 9
Gradle may not work and you need to tweak it - specially multi module projects
Reactive Programming in Java 9
Reactive programming is good and Java 9 is equipped with Reactive Streams API. You will find the reactive version 1.0.0 (org.reactivestreams:reactive-stream:1.0.0) available. Reactive programming comprises few key components which is primarily targeted for asynchronous programming where Reactive API will help to communicate between two reactive programmings. There is a publisher and there is a subscriber and we have a processing which combines subscriber and processor. Subscription which helps to manage the relationship between publisher and subscriber. The implementation of Reactive Steams includes following items:
- Akka Streams
- MondoDB Reactive Java Driver
- Reactive Rabbit
These libraries are using ractive stream and they can talk to each other without caring about their implementation if they are talking using same stream API.
Java 9 has included this reactive stream with same interface and same signature but under a different package and that makes the integration little difficult. If you are RxJava as publisher and you have Java 9 subscriber, I would org.ractive.publisher than java.util.concurrent.Publisher and we need to have the adaptor to make it work at this stage, however in future if all goes to single interface, it would work smoothly.
Reactive Stream are having superior feature over Java 8 Stream and you can do some fantastic stuff with it. These items are not possible with Java 8 Stream. Reactive stream comes with power like multiple stream, multiple consumer, backpressuer and a very different concurrency model.
269: Convenience Factory Methods for Collection
This is one of the feature of Java 9 for real word and it is worth giving enough attention as it is going to be most used feature. It is not as exciting as Lamda Expression in Java 8, but once you know about it, you can not ignore this piece. If you see the example below, you would find it silly but sometime you have to setup a lot of static map and Java 9 makes your life much easy without having that nasty static block. This kind of factory method features are good for testing practices.
213: Private Methods on Interface
Interfaces are suppose
to define public API and would be the face of the services and why would you make them private. In Java 8, we have got default method and static method in interfaces. Now if you follow the below programming which has 2 static methods inside an interface and they have a common code which I want to make the reusable. In Java 8, I can write only public method and make use of it, but what if you don’t want to expose it to public and in such cases to protect your logic, you can define a private method in interface which is only visible to that scope.
New Method on Streams API (Over Java 8 Stream)
Java 8 stream allows you to work on infinite stream but you can not stop that and this is a problem. Now with Java 9, you will get lot of new methods which will give you better control. If you are doing something, you can react on them based on some of the variable value is true during the flow. These methods are named as takeWhile() and dropWhile(). There are cases, like reading a file from tweet and it is an infinite stream, you can not terminate it and it is a bit challenging programming, but with Java 9 real world implementation, it is better equipped to handle such situation.
259: Stack Walking API
This is one more powerful feature which you would certainly want to explore and use it. This allows you to pick your stack at any point of time during the execution without really using the throwable object and use stream to find the particular package name if it has showed up or not. You can use the power of stream and walk through the stack and apply different logic to build something. You also would like to analyse how deep my packages are for certain cases.
238: Multi Release Java File
This is something different you and I had never thought. With this feature in Java 9, it is possible to build multiple java archive where one jar can be prepare targeting jdk 9 and other can be pre-java 9. A good feature for java library builder community. Lets take an example of reactive programming where you would decide if you are using java 9 use reactive java 9 api and if not then use org.reactive.stream.
102: Process API Updates
With this API, you can get the PID (Process ID) and you can kill it if you want to and this is amazing feature for certain scenario. If you are in micro services world, this can surely assist you to manager and program beter.
277 : Updated Deprecation
This feature will help you to see the schedule when a particular deprecated API is plan for deletion and you can plan should you use it or not by looking at the deprecated to deletion journey.
224 : HTML5 Javadoc
It is trivial item and for those organisation who does document heavy enterprise development. Also add value to library development community where they would get the javadoc generate in HTML5 version.
225: Javadoc Search
Now there is a search bar in javadoc, nothing great
Bunch of Stuff Going Awary from Java & compilation may break
- 214: Remove Deprecated GC Combinations
- 231: Remove Launch-time JRE Version Selection
- 240: Remove the JVM TI hprof Agend
- 241: Remove the jhat Tool
- 260: Encapsulate Most Internal API
- 289: Decprecate the Applet API
- 298: Remove Demos and Samples
Possible Unexpected Behaviour
- 158: Unified JVM Logging - If you code is already tuned for it, it may not add any value.
- 223: New Version-Stream Scheme
- 245: Validation JVM Command-Line Flag Agruments
- 248: Make G1 the Default Garbage Collector
- 271: Unified GC Logging
Conclusion : Java 9 Gain
- Encourages good design - Molecularity and encapsulation enforces
- Encourages new way of work with REPL & Reactive Streams
- Nicer feature to make your code simpler and effective - Collection features, walker API, process API etc