What’s New in Java 9?

Af­ter a cou­ple of de­lays, Java 9 is fi­nally here. Three years af­ter Java 8, it comes with some of the big­gest changes to the Java plat­form, adding more than 150 new fea­tures. In this ar­ti­cle, we will cover some of the ma­jor changes.

OpenSource For You - - Contents -

The mod­ule sys­tem in Java 9 is one of the most talked about and awaited fea­ture. So, what’s it all about? Java pro­vides an en­cap­su­la­tion mech­a­nism with the help of ac­cess mod­i­fiers, and any ad­e­quately sized Java project will have classes across mul­ti­ple pack­ages. If the project is large enough, you will have de­pen­den­cies span­ning mul­ti­ple JAR files. Two prob­lems with this ap­proach are: Not all the classes de­clared ‘pub­lic’ are part of the APIs that you might want to ex­pose. What if two JARs have the same class de­clared un­der the same pack­age (split pack­ages)? This can lead to class load­ing is­sues that you will get to know about only at the run­time. To solve these is­sues, Java 9 has in­tro­duced the con­cept of mod­ules. A mod­ule is a higher-level ab­strac­tion over pack­ages. To cre­ate a mod­ule, you need to add a mod­ule­info.java file to your project, and ex­plic­itly de­fine the de­pen­den­cies on other mod­ules us­ing the re­quires key­word as well as the pack­ages from which you want to ex­pose the pub­lic classes us­ing the ex­ports key­word. Do note that other pub­lic classes in­side any pack­age(s) will not be ac­ces­si­ble out­side this mod­ule, other than the ones in­side the ex­ported pack­ages. A sam­ple pack­age def­i­ni­tion will look like what fol­lows:

mod­ule my-mod­ule { ex­ports com.ex­am­ple.java9.class­es_­to_be_­ex­posed_as_apis; re­quires java.base;


JLink: For creat­ing smaller Java run­times

Java run­time has evolved con­sid­er­ably since its in­cep­tion, and thou­sands of classes have been added to it to im­prove func­tion­al­ity, which has turned it into a huge mono­lith. Not all the fea­tures are used by most of the pro­grams at any given time. For ex­am­ple, un­til Java 8, you could cre­ate a small ‘Hello world’ pro­gram that only used classes from the java. lang pack­age, but the run­time would still in­clude all the fea­tures (xml parsers, log­ging APIs, SQL func­tion­al­ity, etc) avail­able in Java.

The JDK it­self was rewrit­ten us­ing the mod­ule sys­tem, so there is no sin­gle rt.jar file that has all the fea­tures of Java; and it has been refac­tored into small in­di­vid­ual mod­ules. This means that you can cre­ate cus­tom run­times that only in­clude the parts of JDK that are used in your mod­ule. Once you have a Java mod­ule JAR file, you can use the JLink tool in the JDK to gen­er­ate the run­time:

Pri­vate meth­ods in­side in­ter­faces

Java 8 in­tro­duced fea­tures with which you could have de­fault func­tion im­ple­men­ta­tions in­side an in­ter­face but all the de­fault im­ple­men­ta­tions be­haved like pub­lic meth­ods. Well, now you can have pri­vate meth­ods in­side the in­ter­faces that can be used by the de­fault meth­ods for bet­ter code or­gan­i­sa­tion.

pub­lic in­ter­face Car { void setCarColor(String color);// nor­mal in­ter­face method de­fault void startCar () {

// de­fault method

Sys­tem.out.println(“Car is start­ing...”); startEngine();

Sys­tem.out.println(“Car started! “);


pri­vate void startEngine() {

// Pri­vate method not avail­able out­side the in­ter­face as an API

Sys­tem.out.println(“En­gine Started “);

} }

jlink --mod­ule-path <mod­ulepath> --add-mod­ules <mod­ules> --limit-mod­ules <mod­ules> --out­put <path>

You can find the de­pen­dent mod­ules re­quired by your mod­ule us­ing the jdeps tool. Just to give you some per­spec­tive, the stan­dard JDK 9 run­time is around 430MB and for a sim­ple ‘Hello World’ ap­pli­ca­tion, the run­time gen­er­ated us­ing the above com­mand is around 24MB, which is great for de­vices with mem­ory re­stric­tions, for IoT ap­pli­ca­tions and for cus­tom run­times to run in­side con­tain­ers like Docker.

JShell: The in­ter­ac­tive Java REPL

Like Python, PHP and sev­eral other lan­guages, now Java too fea­tures an in­ter­ac­tive Read-Eval-Print-Loop. To get started, just type ‘jshell’ in the con­sole and start typ­ing your Java code:

C:\Pro­gram Files\Java\jdk-9\bin>jshell

| Wel­come to JShell -- Ver­sion 9-ea

| For an in­tro­duc­tion type: /help in­tro

jshell> int x= 10 x ==> 10

jshell> Sys­tem.out.println(“value of x is: “+ x); value of x is: 10

Na­tive HTTP/2 and Web sock­ets

Fi­nally, the age old HttpURLCon­nec­tion has been re­placed with the new HttpClient in Java 9, which pro­vides out-ofthe-box sup­port for the HTTP/2 pro­to­col, Web sock­ets and async HTTP re­quests.

HttpClient client = HttpClient.newHttpClient();

HttpRe­quest req =

HttpRe­quest.newBuilder(URI.cre­ate(“http://met­alop. com”)) .header(“User-Agent”,”Java”) .GET()


HttpRe­sponse<String> re­sponse = client.send(req, HttpRe­sponse.BodyHan­dler.asString()); Sys­tem.out.println(re­sponse.sta­tusCode()); Sys­tem.out.println(re­sponse.body());

Beau­ti­ful, isn’t it? No In­putStream or Reader is in­volved — in­stead, the API of­fers a BodyHan­dler which al­lows us to read the string di­rectly from the re­sponse.

En­hance­ments to @Dep­re­cated an­no­ta­tion

Dep­re­cated an­no­ta­tion is one of the three orig­i­nal builtin an­no­ta­tions in Java. Over the years, users re­alised that it is quite am­bigu­ous in na­ture. It didn’t pro­vide a lot of in­for­ma­tion pre­vi­ously, like why some­thing was dep­re­cated; could it be used or would it be re­moved in fu­ture ver­sions of the li­brary; if it was to be re­moved, then at which ver­sion would this oc­cur, etc. So, now the dep­re­cated an­no­ta­tion pro­vides ad­di­tional fields like forRe­moval and since for more lu­cid­ity. There was an­other small en­hance­ment whereby, if you were us­ing a dep­re­cated class and sup­pressed the warn­ing, there would still be a warn­ing left in the place it was used due to the im­port state­ment, and there was no way to an­no­tate the im­port state­ment. This is­sue has also been fixed in the cur­rent re­lease of Java 9.

Multi-re­lease JAR files

Mul­ti­ple, Java-re­lease-spe­cific ver­sions of class/re­source files can now co­ex­ist in the same JAR file. This is good news for li­brary main­tain­ers, who will not be limited to us­ing the min­i­mum sub­set of the API pro­vided by the least ver­sion of Java they want to sup­port.

A multi-re­lease JAR file is one whose MAN­I­FEST.MF file in­cludes the en­try Multi-Re­lease: true in its main sec­tion. Also, META-INF con­tains a ver­sions sub­di­rec­tory whose in­te­ger-named sub­di­rec­to­ries — start­ing with 9 (for Java 9) — store ver­sion-spe­cific class and re­source files. JEP 238 of­fers the fol­low­ing (en­hanced) ex­am­ple:

JAR con­tent root A.class B.class C.class D.class META-INF MAN­I­FEST.MF ver­sions

9 A.class B.class

If this JAR is read by JDK 9, it will use classes A and B for Java 9 in­side the 9 di­rec­tory, but if it is read by a pre-Java 9 JDK, it will read classes from the root.

Other up­dates in Java 9

Java 9 packs sev­eral other up­dates. For in­stance, JavaDocs now sup­ports HTML 5 and the search fea­ture, and new meth­ods have been added to the Stream in­ter­face: dropWhile, takeWhile, ofNul­lable. The it­er­ate method now also al­lows you to pro­vide a pred­i­cate on when to stop it­er­at­ing.

In­tStream.it­er­ate(1, i -> i <=10, i -> i + 1).forEach(Sys­tem. out::println);

The col­lec­tions frame­work has been up­dated to now al­low a pop­u­la­tion of col­lec­tions at the time of cre­ation, as shown below:

List<In­te­ger> in­te­gers = List.of(1, 2, 3);

There are sev­eral other new fea­tures in Java 9. To get a com­pre­hen­sive list of all the new ad­di­tions to Java 9, you can visit the re­lease notes at https://docs.or­a­cle.com/javase/9/ what­snew/toc.htm. By: Shiva Sax­ena The au­thor is a FOSS en­thu­si­ast. He cur­rently works as a con­sul­tant, and is in­volved in de­vel­op­ing en­ter­prise ap­pli­ca­tion and Soft­ware-as-a-Ser­vice (SaaS) prod­ucts. He has hand­son de­vel­op­ment ex­pe­ri­ence with An­droid, Apache Camel, C#, .NET, Hadoop, HTML5, Java, OData, PHP, Re­act, etc, and loves to ex­plore new and bleed­ing-edge tech­nolo­gies. He can be reached at shiv­asax­ena@out­look.com.

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.