2. WHAT IS NEW IN JAVA 9
Java 9 is a major release. It may seem to be a maintenance release
that pushes forward project Jigsaw Project (Module System). āØ
But along with the new module system and a number of internal
changes associated with it Java 9 brings also a number of cool
new stuff to the developerās toolbox.
1. New Features in Java Language
2. New Features in Java Compiler
3. New Features in Java Libraries
4. New Features in Java Tools
5. New Features in Java Runtime (JVM)
2
3. WHAT IS NEW IN JAVA 9
1. NEW FEATURES IN JAVA LANGUAGE
1. Private methods in Interfaces
2. Java 9 Module System
3. Enhanced @Deprecated annotation
4. Diamond Operator for Anonymous Inner Class
5. Try With Resources Improvement
6. Multi-release JARs
3
4. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.1. PRIVATE METHODS IN INTERFACES
ā£ In Java 8, we can provide method implementation in Interfaces using default and static methods.
However we cannot create private methods in interfaces.
ā£ To avoid redundant code and more re-usability, Java 9 supports private methods in Java SE 9
Interfaces.
ā£ These private methods are like other class private methods only, there is no difference between
them.
ā£ In Java 9 and later versions, an interface can have six kinds of things:
- Constant variables
- Abstract methods
- Default methods
- Static methods
- Private methods
- Private static methods
4
5. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.1. PRIVATE METHODS IN INTERFACES
public interface DBLogging {
String MONGO_DB_NAME = "ABC_Mongo_Datastore";āØ
String NEO4J_DB_NAME = "ABC_Neo4J_Datastore";āØ
String CASSANDRA_DB_NAME = "ABC_Cassandra_Datastore";
default void logInfo(String message) {āØ
log(message, āINFO");āØ
}
default void logWarn(String message) {āØ
log(message, "WARN");āØ
}
default void logError(String message) {āØ
log(message, "ERROR");āØ
}āØ
āØ
private void log(String message, String msgPreļ¬x) {āØ
// ā¦āØ
}
5
6. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.1. PRIVATE METHODS IN INTERFACES
ā£ Rules to deļ¬ne private methods in an Interface?
- We should use private modiļ¬er to deļ¬ne these methods.
- No private and abstract modiļ¬ers together, it will give compiler
error. It is not a new rule. We cannot use the combination of private
and abstract modiļ¬ers, because both have different meanings
- Private methods must contain body.
- No lesser accessibility than private modiļ¬er. These interface
private methods are useful or accessible only within that interface
only. We cannot access or inherit private methods from an
interface to another interface or class
6
7. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ One of the big changes or java 9 feature is the Module
System.
ā£ Before Java SE 9 versions, we are using monolithic jars to
develop java-based applications. This architecture has lot
of limitations and drawbacks. To avoid all these
shortcomings, Java SE 9 is coming with Module System
ā£ JDK 9 is coming with almost 100 modules. We can use
JDK Modules and also we can create our own modules.āØ
7
8. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ Java SE 8 or earlier systems have following problems in developing or
delivering Java Based application:
- As JDK is too big, it is a bit tough to scale down to small devices.
- JAR ļ¬les like rt.jar etc are too big to use in small devices and applications.
- As JDK is too big, our applications or devices are not able to support
better performance.
- There is no strong encapsulation in the current Java System because
āpublicā access modiļ¬er is too open. Everyone can access it.
- As JDK, JRE is too big, it is hard to test and maintain applications.
- Its a bit tough to support less coupling between components
8
9. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ Java SE 9 module system is going to provide the following beneļ¬ts:
- As Java SE 9 is going to divide JDK, JRE, JARs etc, into smaller modules, we can use whatever
modules we want. So it is very easy to scale down the java application to small devices.
- Ease of testing and maintainability.
- Supports better performance.
- As public is not just public, it supports very strong encapsulation. We cannot access internal
non-critical APIs anymore.
- Modules can hide unwanted and internal details very safely, we can get better security.
- Application is too small because we can use only what ever modules we want.
- Its easy to support less coupling between components
- Its easy to support single responsibility principle (SRP).
ā£ The main goal of Java 9 module system is to support Modular Programming in Java.
9
10. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ We know what a JDK software contains. After installing JDK 8 software, we can see a
couple of directories like bin, jre, lib etc in java home folder. Oracle has changed this
folder structure a bit differently.
ā£ JDK 9 does NOT contain jre directory in JDK 9 anymore.
ā£ Instead of jre folder, JDK 9 software contains a new folder ājmodsā. It contains a set
of Java 9 modules.
ā£ In JDK 9, No rt.jar and No tools.jar
ā£ All JDK modules starts with ājdk.*ā
ā£ All Java SE speciļ¬cations modules starts with ājava.*ā
ā£ Java 9 module system has a ājava.baseā module. Itās known as base module. Itās an
independent module and does NOT dependent on any other modules. By default, all
other modules dependent on this module. We can think of it like java.lang package.
10
11. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
11
12. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ We have already developed many java applications using
Java 8 or earlier version of java. We know how a Java 8 or
earlier applications looks like and what it contains.
ā£ In brief, A Java 8 applications in a diagram as shown
below:
12
13. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ Java 9 applications does not have much difference with this. It just
introduced a new component called āModuleā, which is used to
group a set of related packages into a group. And one more new
component that module descriptor (āmodule-info.javaā).
ā£ Rest of the application is same as earlier versions of applications
as shown below:
13
14. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ Modular JAR ļ¬les contain an additional module descriptor named
module-info.java
ā£ In this module descriptor, dependencies on other modules are
expressed through ārequiresā statements.
ā£ Additionally, `exports` statements control which packages are
accessible to other modules.āØ
āØ
module com.bkn.demo {āØ
exports com.bkn.demo; āØ
requires java.sql;āØ
requires java.logging;āØ
}
14
15. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ The exports keyword indicates that these packages are
available to other modules. This means that public classes are,
by default, only public within the module unless it is speciļ¬ed
within the module info declaration. (module-info.java). All non-
exported packages are encapsulated in the module by default
ā£ The requires keyword indicates that this module depends on
another module.
ā£ The uses keyword indicates that this module uses a service.
ā£ When starting a modular application, the JVM veriļ¬es whether
all modules can be resolved based on the `requires` statements
15
16. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ List modules: In order to see which modules are available within Java, we can enter the following command:āØ
āØ
java --list-modulesāØ
āØ
A list is shown with the available modules. Below is an extract from the list:āØ
java.activation@9āØ
java.base@9āØ
java.compiler@9āØ
...āØ
āØ
From the list, we can see that the modules are split into four categories: the ones starting with java (standard Java modules), the
ones starting with javafx (JavaFX modules), the ones starting with jdk (JDK-speciļ¬c modules) and the ones starting with
oracle (Oracle-speciļ¬c modules)
ā£ Describe Module: Module properties are located in a module-info.java ļ¬le. In order to see the description of the module
deļ¬ned in this ļ¬le, the following command can be used:āØ
āØ
java --describe-module java.sqlāØ
āØ
This will output the following:āØ
java.sql@9āØ
exports java.sqlāØ
exports javax.sqlāØ
requires java.logging transitiveāØ
uses java.sql.Driver
16
17. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ Compilation: With the following command, we compile the
application. The -d option speciļ¬es the destination directory for the
compiled classes.āØ
āØ
javac -d out module-dir/com/bkn/demo/Demo.java module-dir/
module-info.java
ā£ Execution: To execute the compiled classes, we need to set the
module-path. This option sets the directories where the modules can
be foundāØ
It is necessary to set the module and the package and the class where
the main class is locatedāØ
āØ
java --module-path out āmodule com.bkn.demo.Demo
17
18. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
ā£ Create a JAR ļ¬le: We can create the JAR ļ¬le with the command below. āØ
The ļ¬le option speciļ¬es the location and name of the JAR ļ¬le. The main-class option speciļ¬es the
entry point of the application. The C option speciļ¬es the location of the classes to include in the
JAR ļ¬le.āØ
āØ
jar -c -f target/demo-module.jar -C out .
ā£ Create your own JRE: The directory structure of the JDK has changed a bit. A directory, jmods, is
added. This directory contains a jmod ļ¬le for each module.āØ
A jmod ļ¬le is like a JAR ļ¬le, but for modulesāØ
āØ
The size of the whole jre is ~179 MB on my laptop. We can create our own JRE with only the
modules we are using in our application with the following command:āØ
āØ
jlink --module-path ../jmods --add-modules java.base --output path/jreāØ
āØ
The size of newly created runtime is only 38 MB.
ā£ Letās look into jdk directories & try to create, compile and execute a moduleā¦
18
19. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.3. ENHANCED @DEPRECATED ANNOTATION
ā£ In Java SE 8 and earlier versions, @Deprecated annotation is just a Marker
interface without any methods. It is used to mark a Java API that is a class, ļ¬eld,
method, interface, constructor, enum etc.
ā£ In Java SE 9, @Deprecated annotation provides more information about
deprecated API.
ā£ Two methods added to this Deprecated interface: forRemoval and since to
serve this information.
ā£ A method forRemoval() returning a boolean. If true, it means that this API
element is earmarked for removal in a future release. If false, the API element is
deprecated, but there is currently no intention to remove it in a future release
ā£ A method named since() returning String. This string should contain the
release or version number at which this API became deprecated
19
20. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.4. DIAMOND OPERATOR FOR ANONYMOUS INNER CLASS
ā£ Java SE 8 has a limitation in the use of Diamond operator with Anonymous Inner Class. Now diamond operator can be used with
anonymous inner class. Example:āØ
āØ
abstract class MyHandler<T> {āØ
private T content;āØ
public MyHandler(T content) {āØ
this.content = content;āØ
System.out.println("Constructor for MyHandler with content: " + content.toString());āØ
}āØ
public T getContent() {āØ
return content;āØ
}āØ
public void setContent(T content) {āØ
this.content = content;āØ
}āØ
abstract void handle();āØ
}āØ
āØ
ā¦ āØ
āØ
MyHandler<?> handler = new MyHandler<>("One hundred") {āØ
@OverrideāØ
void handle() {āØ
System.out.println("handle : " + getContent());āØ
}āØ
};
20
21. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.5. TRY WITH RESOURCES IMPROVEMENT
ā£ Try-with-resources was a great feature introduced in Java 7 to automatically manage resources using an
AutoCloseable interface. This helps a lot, of course, as we have no need to close the resources explicitly in our code
ā£ Java 7:āØ
āØ
Connection dbCon = DriverManager.getConnection(ā¦);āØ
try (ResultSet resultSet = dbCon.createStatement().executeQuery(āā¦ā)) {āØ
// ā¦āØ
} catch (SQLException e) {āØ
//ā¦āØ
} ļ¬nally {āØ
if (null != dbCon)āØ
dbCon.close();āØ
}
ā£ Java 9:āØ
āØ
Connection dbCon = DriverManager.getConnection(...);āØ
try (dbCon; ResultSet resultSet = dbCon.createStatement().executeQuery(...)) {āØ
// ...āØ
} catch (SQLException e) {āØ
// ...āØ
}
21
22. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA TOOLS
1.6. MULTI-RELEASE JARS
ā£ When a new version of Java comes out, it takes years for all users of your library to switch to this new version
ā£ That means the library has to be backward compatible with the oldest version of Java you want to support (e.g., Java 6 or
7 in many cases).
ā£ That effectively means you won't get to use the new features of Java 9 in your library for a long time.
ā£ Fortunately, the multi-release JAR feature allows you to create alternate versions of classes that are only used when
running the library on a speciļ¬c Java versionāØ
āØ
multirelease.jarāØ
āāā META-INFāØ
ā āāā versionsāØ
ā āāā 9āØ
ā āāā multireleaseāØ
ā āāā Helper.classāØ
āāā multireleaseāØ
āāā Helper.classāØ
āāā Main.class
ā£ In this case, multirelease.jar can be used on Java 9, where instead of the top-level multirelease.Helper class, the one under
`META-INF/versions/9` is used. This Java 9-speciļ¬c version of the class can use Java 9 features and libraries. At the same
time, using this JAR on earlier Java versions still works, since the older Java versions only see the top-level Helper class.
22
23. WHAT IS NEW IN JAVA 9
2. NEW FEATURES IN JAVA COMPILER
1. Ahead-of-Time Compilation (AoT)
23
24. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA COMPILER
2.1. AHEAD-OF-TIME COMPILATION (AOT)
ā£ One of the main goals while designing the java language was to make application
portability a reality.āØ
āØ
Java was designed in such a way that the same .class ļ¬les created in one operating system can
run seamlessly in another operating system or another computer without fail as long as the
target system has a working Java Runtime Environment installed in it.āØ
āØ
Initially released Java runtimes had signiļ¬cantly slower performance when compared to other
languages and their compilers such as C and C++.
ā£ To give better performance at runtime, complex dynamic compilers called Just-In-Time(JIT)
were introduced.āØ
āØ
JIT compilers selectively takes bytecodes of most frequently used methods in the application
and convert them to native code during runtimeāØ
āØ
When method is compiled, JVM directly calls compiled methodās executable machine code
rather than interpreting bytecode line by line.
24
25. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA COMPILER
2.1. AHEAD-OF-TIME COMPILATION (AOT)
ā£ But there are certain drawbacks with this approach (JIT). In case of a large and
complex Java application, JIT compilers may take a long time to warm up.āØ
āØ
Java applications may contain methods that are not frequently used and hence
never compiled at all. These methods may have to be interpreted while invoked.āØ
āØ
Due to repeated interpreted invocations, there could be performance problems
ā£ Graal is an Oracle project aimed at implementing a high performance dynamic
Java compiler and interpreter so that JVM based languages can achieve
performance of native languages during runtime.āØ
āØ
There is a Java Enhancement Proposal [JEP 295] which has been put forward to
use Graal with Java for Ahead Of Time Compilation.
25
26. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA COMPILER
2.1. AHEAD-OF-TIME COMPILATION (AOT)
ā£ It is not ofļ¬cially supported by Java 9 but is added as an experimental feature.āØ
āØ
Furthermore, AOT is currently restricted to 64 bit Linux based systems.āØ
āØ
Ahead Of Time compilation is done using a new tool jaotc instead of javac.
ā£ To use Ahead Of Time compilation, users need to use the same JDK for
compilation and execution.āØ
āØ
Version information about jaotc used for compilation is added as a part of the
libraries and are checked during load time.āØ
āØ
If the Java runtime is updated, you need to recompile the AOT compiled
modules before executing them. Mismatch in jdk versions used for compilation
and execution may result in application crashes.
26
27. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA COMPILER
2.1. AHEAD-OF-TIME COMPILATION (AOT)
ā£ Lambda expressions and other complex concepts of Java, which
uses dynamically generated classes at runtime are not currently
supported by AOT compiler.
ā£ To generate shared object (.so) ļ¬les, the system needs libelf to be
pre-installed
ā£ Java AOT compiler can be invoked in same way as javac:āØ
āØ
jaotc āoutput libTest.so Test.class
ā£ To execute the above AOT compiled binaries, we can execute:āØ
āØ
java āXX:AOTLibrary=./libTest.so Test
27
28. WHAT IS NEW IN JAVA 9
3. NEW FEATURES IN JAVA LIBRARIES
1. Process API Improvements
2. Optional Class Improvements
3. Stream API Improvements
4. Collection factory methods
5. HTTP 2 Client
6. Reactive Streams
7. Multi-Resolution Image API
28
29. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LIBRARIES
3.1. PROCESS API IMPROVEMENTS
ā£ Java SE 9 is coming with some improvements in Process API. Couple new classes and methods have been added to ease the
controlling and managing of OS processes.
ā£ Two new interfaces in Process API:
- java.lang.ProcessHandle: Helps to handle and control processes. We can monitor processes, list its children, get
information etc.
- java.lang.ProcessHandle.Info: It is added to Java 9, and used to provide information about the process. It is nested
interface of ProcessHandle:
ProcessHandle self = ProcessHandle.current();āØ
System.out.println("Process Id: ā + self.pid());āØ
System.out.println("Direct children: "+ self.children());āØ
System.out.println("Class name: ā + self.getClass());āØ
System.out.println("All processes: ā + ProcessHandle.allProcesses());āØ
System.out.println("Process info: ā + self.info());āØ
System.out.println("Is process alive: ā + self.isAlive()); āØ
System.out.println("Process's parent ā + self.parent());āØ
āØ
ProcessHandle.Info processInfo = self.info();āØ
Optional<String[]> args = processInfo.arguments();āØ
Optional<String> cmd =Ā processInfo.commandLine();āØ
Optional<Instant> startTime = processInfo.startInstant();āØ
Optional<Duration> cpuUsage = processInfo.totalCpuDuration();
29
30. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LIBRARIES
3.2. OPTIONAL CLASS IMPROVEMENTS
ā£ In Java SE 9, Oracle Corp has introduced the following three methods
to improve Optional functionality.
- stream()
- ifPresentOrElse()
- or()
ā£ stream() : if a value present in the given Optional object, this stream()
method returns a sequential Stream with that value. Otherwise, it
returns an Empty Stream.āØ
āØ
Stream<Optional> employee = getEmployee(id);āØ
Stream employeeStream = emp.ļ¬atMap(Optional::stream);
30
31. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LIBRARIES
3.2. OPTIONAL CLASS IMPROVEMENTS
ā£ ifPresentOrElse(): In Java SE 8, we should use ifPresent(),
isPresent(), orElse() etc. methods to check an Optional
object and perform some functionality on it.āØ
ifPresentOrElse method combines all those methods.
ā£ or(): If a value is present, returns an Optional describing
the value, otherwise returns an Optional produced by the
supplying function
31
32. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LIBRARIES
3.3. STREAM API IMPROVEMENTS
ā£ The Streams API is arguably one of the best improvements to the Java standard
library in a long time. It allows you to create declarative pipelines of transformations
on collections. With Java 9, this only gets better.
ā£ In Java SE 9, Oracle Corp has added four useful new methods to java.util.Stream
interface.
ā£ As Stream is an interface, all those new implemented methods are default methods.
1. dropWhile
2. takeWhile
3. ofNullable
4. iterate (new overloaded method)
ā£ These are very useful methods in writing some functional style code.
32
33. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LIBRARIES
3.4. COLLECTION FACTORY METHODS
ā£ Often you want to create a collection (e.g., a List or Set) in your code and
directly populate it with some elements. That leads to repetitive code where
you instantiate the collection, followed by several `add` calls.
ā£ With Java 9, several so-called collection factory methods have been addedāØ
āØ
Set<Integer> ints = Set.of(1, 2, 3);
ā£ The Set returned above is JVM internal
class:java.util.ImmutableCollections.SetN, which extends public
java.util.AbstractSet. It is immutable.
ā£ if we try to add or remove elements, an UnsupportedOperationException
will be thrown.
ā£ You can also convert an entire array into a Set with the same method.
33
34. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LIBRARIES
3.5. HTTP 2 CLIENT
ā£ A new way of performing HTTP calls arrives with Java 9.
ā£ New HTTP 2 Client API supports HTTP/2 protocol and WebSocket features with
performance that should be comparable with the Apache HttpClient, Netty and Jetty.
ā£ As existing or Legacy HTTP Client API has numerous issues like supports HTTP/1.1
protocol and does not support HTTP/2 protocol and WebSocket, works only in
Blocking mode and lot of performance issues.
ā£ It supports both Synchronous (Blocking Mode) and Asynchronous Modes. It supports
Asynchronous Mode using WebSocket API.
ā£ HttpClient provides new APIs to deal with HTTP/2 features such as streams and
server push.
ā£ One caveat: The new HttpClient API is delivered as a so-called _incubator module_
in Java 9. This means the API isn't guaranteed to be 100% ļ¬nal yet.
34
35. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LIBRARIES
3.6. REACTIVE STREAMS
ā£ Now-a-days, Reactive Programming has become very popular in
developing applications to get some beautiful beneļ¬ts. Scala, Play, Akka
etc. Frameworks has already integrated Reactive Streams and getting many
beneļ¬ts.
ā£ Reactive Streams allows us to implement non-blocking asynchronous stream
processing. This is a major step towards applying reactive programming
model to core java programming.
ā£ If you are new to reactive programming, please read Reactive Manifesto.
ā£ Oracle Corps is also introducing new Reactive Streams API in Java SE 9.
ā£ Java SE 9 Reactive Streams API is a Publish/Subscribe Framework to
implement Asynchronous, Scalable and Parallel applications very easily using
Java language.
35
36. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LIBRARIES
3.6. REACTIVE STREAMS
ā£ Java SE 9 has introduced the following API to develop Reactive Streams in Java-based
applications
- java.util.concurrent.Flow
- java.util.concurrent.Flow.Publisher
- java.util.concurrent.Flow.Subscriber
- java.util.concurrent.Flow.Processor
ā£ Reactive Streams is about asynchronous processing of stream, so there should be a Publisher
and a Subscriber.
ā£ The Publisher publishes the stream of data and the Subscriber consumes the data.
ā£ Sometimes we have to transform the data between Publisher and Subscriber. Processor is the
entity sitting between the end publisher and subscriber to transform the data received from
publisher so that subscriber can understand it.
ā£ Example: reactive
36
37. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LIBRARIES
3.7. MULTI-RESOLUTION IMAGE API
ā£ The interface java.awt.image.MultiResolutionImage encapsulates a set of images
with different resolutions into a single object.
ā£ We can retrieve a resolution-speciļ¬c image variant based on a given DPI metric and
set of image transformations or retrieve all of the variants in the image.
ā£ The java.awt.Graphics class gets variant from a multi-resolution image based on the
current display DPI metric and any applied transformations.
ā£ The class java.awt.image.BaseMultiResolutionImage provides basic
implementation:āØ
āØ
BufferedImage[] resolutionVariants = ā¦.āØ
MultiResolutionImage bmrImage = new BaseMultiResolutionImage(baseIndex,
resolutionVariants);āØ
Image testRVImage = bmrImage.getResolutionVariant(16, 16);āØ
37
38. WHAT IS NEW IN JAVA 9
4. NEW FEATURES IN JAVA TOOLS
1. JShell: The interactive Java REPL
2. Jlink: Linking
3. JCMD Improvements
38
39. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA TOOLS
4.1. JSHELL: THE INTERACTIVE JAVA REPL
ā£ Itās an interactive tool to evaluate declarations, statements, and
expressions of Java, together with an API.
ā£ It is very convenient for testing small code snippets, which
otherwise require creating a new class with the main method.āØ
āØ
jdk-9bin>jshell.exeāØ
| Welcome to JShell -- Version 9āØ
| For an introduction type: /help introāØ
āØ
jshell> "This is my long string. I want a part of it".substring(8,19);āØ
$5 ==> "my long string"
39
40. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA TOOLS
4.2. JLINK: LINKING
ā£ When you have modules with explicit dependencies, and a
modularized JDK, new possibilities arise.
ā£ Your application modules now state their dependencies on other
application modules and on the modules it uses from the JDK
ā£ Why not use that information to create a minimal runtime
environment, containing just those modules necessary to run your
application?
ā£ That's made possible with the new jlink tool in Java 9
ā£ Instead of shipping your app with a fully loaded JDK installation, you
can create a minimal runtime image optimized for your application.
40
41. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA TOOLS
4.2. JCMD IMPROVEMENTS
ā£ Letās explore some of the new subcommands in jcmd command line utility. We will get a list of all classes loaded in
the JVM and their inheritance structure.
ā£ In the example below we can see the hierarchy of java.lang.Socket loaded in JVM running Eclipse Neon:āØ
āØ
jdk-9bin>jcmd 14056 VM.class_hierarchy -i -s java.net.SocketāØ
14056:āØ
java.lang.Object/nullāØ
|--java.net.Socket/nullāØ
| implements java.io.Closeable/null (declared intf)āØ
| implements java.lang.AutoCloseable/null (inherited intf)āØ
| |--org.eclipse.ecf.internal.provider.ļ¬letransfer.httpclient4.CloseMonitoringSocketāØ
| | implements java.lang.AutoCloseable/null (inherited intf)āØ
| | implements java.io.Closeable/null (inherited intf)āØ
| |--javax.net.ssl.SSLSocket/nullāØ
| | implements java.lang.AutoCloseable/null (inherited intf)āØ
| | implements java.io.Closeable/null (inherited intf)āØ
āØ
The ļ¬rst parameter of jcmd command is the process id (PID) of the JVM on which we want to run the command.āØ
āØ
Another interesting subcommand is set_vmļ¬ag. We can modify some JVM parameters online, without the need
of restarting the JVM process and modifying its startup parameters
41
42. WHAT IS NEW IN JAVA 9
5. NEW FEATURES IN JAVA RUNTIME (JVM)
1. G1 as the default garbage collectorāØ
āØ
42
43. WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA RUNTIME (JVM)
5.1. G1 AS THE DEFAULT GARBAGE COLLECTOR
ā£ One of Java 9ās most contested changes, second only to Project Jigsaw, is that
Garbage First (G1) is the new default garbage collector
ā£ G1 limits pause times and is giving up some throughput to achieve that.
Implementation-wise, it does not separate the heap into continuous spaces like
Eden, young and old but into ļ¬xed-sized regions, where G1 assigns a role to a
region when it starts using it and resets the role once it collected the regionās
entire content. Talking about collections, those focus on the regions with the
most garbage, hence the name, because that promises the least work.
ā£ G1 will identify String instances that have equal value arrays and then make
them share the same array instance. Apparently, duplicate strings are common
and this optimization safes about 10% heap space.āØ
āØ
43
44. WHAT IS NEW IN JAVA 9
SOURCES
ā£ https://docs.oracle.com/javase/9/whatsnew/toc.htm
ā£ https://www.journaldev.com/13121/java-9-features-with-
examples
ā£ https://www.javatpoint.com/java-9-features
ā£ https://www.pluralsight.com/blog/software-development/
java-9-new-features
ā£ https://www.baeldung.com/new-java-9āØ
āØ
44