Java 9 : What’s new !

Gaurav Kumar Srivastava
stackspacearena
Published in
3 min readSep 19, 2017

Expected finally to be released on Java 9 has been much awaited ever since the release of Java 8 , almost three years back.
September 21st, 2017, Java 9 shall be shipped with a host of new features , ‘Modules’ being the most important of those.
Let us see Modules as well as others in a nutshell

is a named, self-describing collection of code and data. Its code is organized as a set of packages containing types, i.e., Java classes and interfaces; its data includes resources and other kinds of static information.’

Think about scalability. JDK is a bundled set of jars required to be downloaded as whole. What do we do when we try scale? We break into independent pieces,that can communicate with each other as and when required.

Java came up with multiple modules in JDK to make it light weight, and scalable.

With Help of Linker , these modules could be linked together at runtime , thus making the age old and heavy RT.jar unnecessary.
If we look into the JDK structure now, we find all components now put as modules inside lib/src folder. Each module now contains its own module-info.java

Note the contents in above module-info.java, which is also called Module Descriptor. Java 9 introduces two new statements ; ‘requires’ and ‘export’

‘requires’ is analogous to import . Dependencies on other modules are expressed through`requires` statements.Whereas ‘exports’ specifies the packages inside current module , that are exposed and can be required by other modules .

We can create our own modules in similar pattern and provide dependencies and expose our module’s packages.

‘The jlink tool defines a plugin mechanism for transformation and optimization during the assembly process, and for the generation of alternative image formats. It can create a custom runtime optimized for a single program. defines link time as an optional phase between the phases of compile time and run time. Link time requires a linking tool that assembles and optimizes a set of modules and their transitive dependencies to create a runtime image or executable.’ — from oracle docs.

Java 9 introduces a runtime linking tool , which can map all dependencies between different modules at runtime .

A basic invocation of jlink has following format :

$ jlink --module-path <modulepath> --add-modules <modules> --limit-modules <modules> --output <path>
  • --module-path is the path where observable modules will be discovered by the linker; these can be modular JAR files, JMOD files, or exploded modules
  • --add-modules names the modules to add to the run-time image; these modules can, via transitive dependencies, cause additional modules to be added
  • --limit-modules limits the universe of observable modules
  • --output is the directory that will contain the resulting run-time image

3. JShell :

An interactive Read-Eval-Print-Loop, was long awaited in Java. And Java 9 has come up with jshell to end that wait. The tool provides an interactive command-line interface for evaluating declarations, statements, and expressions of the Java programming language. It facilitates prototyping and exploration of coding options with immediate results and feedback. The immediate feedback combined with the ability to start with expressions is useful for education-whether learning the Java language or just learning a new API or language feature.

One can launch jshell by simply typing ‘jshell’ in command window. for mac , you can simply launch the jshell.exe from /bin/ folder in jdk.

Here is sample window :

4. JAVA Docs :

The new improved Java docs now come with a search option , using which you can easily search any API. The Doc pages are now HTML5 enabled and hence give a better user experience.

5. Java Language Changes

There are minor but multiple change in Java SE 9

Allow effectively-final variables to be used as resources in the try-with-resources statement.

You can use diamond syntax in conjunction with anonymous inner classes.

Underscore character is not a legal name

Private interface methods are supported. This support allows nonabstract methods of an interface to share code between them.

  • Compiler Control
  • Segmented Code Cache
  • Dynamic Linking of Language Defined Object models

GC

  • G1 is the default GC now
  • Unified GC logging
  • Unified JVM loging
  • Concurrent Mask Sweep Alogorithms deprecated

Originally published at https://stackspacearena.blogspot.com on September 19, 2017.

--

--