SlideShare a Scribd company logo
1 of 44
Download to read offline
Ali BAKANā€Ø
Software Engineerā€Ø
ā€Ø
12.09.2018ā€Ø
ali.bakan@cloudnesil.comā€Ø
www.cluoudnesil.com
WHAT IS NEW IN JAVA 9
1
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
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
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
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
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
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
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
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
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
WHAT IS NEW IN JAVA 9 - NEW FEATURES IN JAVA LANGUAGE
1.2. JAVA 9 MODULE SYSTEM
11
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
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
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
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
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
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
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
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
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
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
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
WHAT IS NEW IN JAVA 9
2. NEW FEATURES IN JAVA COMPILER
1. Ahead-of-Time Compilation (AoT)
23
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
WHAT IS NEW IN JAVA 9
5. NEW FEATURES IN JAVA RUNTIME (JVM)
1. G1 as the default garbage collectorā€Ø
ā€Ø
42
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
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

More Related Content

What's hot

Java 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgradeJava 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgradeSimone Bordet
Ā 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
Ā 
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | EdurekaJava Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | EdurekaEdureka!
Ā 
Spring Boot
Spring BootSpring Boot
Spring BootJiayun Zhou
Ā 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets Hitesh-Java
Ā 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambdaManav Prasad
Ā 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
Ā 
Core java concepts
Core java  conceptsCore java  concepts
Core java conceptsRam132
Ā 
Core java
Core javaCore java
Core javaShivaraj R
Ā 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Edureka!
Ā 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11 Knoldus Inc.
Ā 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)arvind pandey
Ā 
OOP java
OOP javaOOP java
OOP javaxball977
Ā 
Introduce yourself to java 17
Introduce yourself to java 17Introduce yourself to java 17
Introduce yourself to java 17ankitbhandari32
Ā 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
Ā 

What's hot (20)

Java 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgradeJava 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgrade
Ā 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
Ā 
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | EdurekaJava Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Ā 
Java Programming
Java ProgrammingJava Programming
Java Programming
Ā 
Spring Boot
Spring BootSpring Boot
Spring Boot
Ā 
Java 17
Java 17Java 17
Java 17
Ā 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
Ā 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
Ā 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
Ā 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
Ā 
Core java
Core javaCore java
Core java
Ā 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Ā 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11
Ā 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
Ā 
OOP java
OOP javaOOP java
OOP java
Ā 
Introduce yourself to java 17
Introduce yourself to java 17Introduce yourself to java 17
Introduce yourself to java 17
Ā 
Java IO
Java IOJava IO
Java IO
Ā 
Generics
GenericsGenerics
Generics
Ā 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Ā 
Java I/O
Java I/OJava I/O
Java I/O
Ā 

Similar to Java 9 New Features

Java 9 / Jigsaw - AJUG/VJUG session
Java 9 / Jigsaw - AJUG/VJUG  sessionJava 9 / Jigsaw - AJUG/VJUG  session
Java 9 / Jigsaw - AJUG/VJUG sessionMani Sarkar
Ā 
JavaOne 2016: Life after Modularity
JavaOne 2016: Life after ModularityJavaOne 2016: Life after Modularity
JavaOne 2016: Life after ModularityDanHeidinga
Ā 
Java 9: Deep Dive into Modularity and Dealing with Migration Issues
Java 9: Deep Dive into Modularity and Dealing with Migration IssuesJava 9: Deep Dive into Modularity and Dealing with Migration Issues
Java 9: Deep Dive into Modularity and Dealing with Migration IssuesGlobalLogic Ukraine
Ā 
Java 9 Modularity and Project Jigsaw
Java 9 Modularity and Project JigsawJava 9 Modularity and Project Jigsaw
Java 9 Modularity and Project JigsawComsysto Reply GmbH
Ā 
Java Platform Module System
Java Platform Module SystemJava Platform Module System
Java Platform Module SystemVignesh Ramesh
Ā 
Java 9 Jigsaw HackDay
Java 9 Jigsaw HackDayJava 9 Jigsaw HackDay
Java 9 Jigsaw HackDayOleg Tsal-Tsalko
Ā 
Modules all the way down: OSGi and the Java Platform Module System
Modules all the way down: OSGi and the Java Platform Module SystemModules all the way down: OSGi and the Java Platform Module System
Modules all the way down: OSGi and the Java Platform Module SystemTim Ellison
Ā 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java WorkshopSimon Ritter
Ā 
Java 9 / Jigsaw - LJC / VJUG session (hackday session)
Java 9 / Jigsaw - LJC / VJUG session (hackday session)Java 9 / Jigsaw - LJC / VJUG session (hackday session)
Java 9 / Jigsaw - LJC / VJUG session (hackday session)Mani Sarkar
Ā 
Java 9 Module System
Java 9 Module SystemJava 9 Module System
Java 9 Module SystemHasan Ɯnal
Ā 
Apache Maven supports ALL Java (Javaland 2019)
Apache Maven supports ALL Java (Javaland 2019)Apache Maven supports ALL Java (Javaland 2019)
Apache Maven supports ALL Java (Javaland 2019)Robert Scholte
Ā 
Java course-in-mumbai
Java course-in-mumbaiJava course-in-mumbai
Java course-in-mumbaiUnmesh Baile
Ā 
Apache Maven supports all Java (JokerConf 2018)
Apache Maven supports all Java (JokerConf 2018)Apache Maven supports all Java (JokerConf 2018)
Apache Maven supports all Java (JokerConf 2018)Robert Scholte
Ā 
What's Expected in Java 7
What's Expected in Java 7What's Expected in Java 7
What's Expected in Java 7Gal Marder
Ā 
Preparing for java 9 modules upload
Preparing for java 9 modules uploadPreparing for java 9 modules upload
Preparing for java 9 modules uploadRyan Cuprak
Ā 

Similar to Java 9 New Features (20)

Java 9 / Jigsaw - AJUG/VJUG session
Java 9 / Jigsaw - AJUG/VJUG  sessionJava 9 / Jigsaw - AJUG/VJUG  session
Java 9 / Jigsaw - AJUG/VJUG session
Ā 
JavaOne 2016: Life after Modularity
JavaOne 2016: Life after ModularityJavaOne 2016: Life after Modularity
JavaOne 2016: Life after Modularity
Ā 
Java 9: Deep Dive into Modularity and Dealing with Migration Issues
Java 9: Deep Dive into Modularity and Dealing with Migration IssuesJava 9: Deep Dive into Modularity and Dealing with Migration Issues
Java 9: Deep Dive into Modularity and Dealing with Migration Issues
Ā 
Java 9 Modularity and Project Jigsaw
Java 9 Modularity and Project JigsawJava 9 Modularity and Project Jigsaw
Java 9 Modularity and Project Jigsaw
Ā 
Java Platform Module System
Java Platform Module SystemJava Platform Module System
Java Platform Module System
Ā 
Java9
Java9Java9
Java9
Ā 
Java 9 Jigsaw HackDay
Java 9 Jigsaw HackDayJava 9 Jigsaw HackDay
Java 9 Jigsaw HackDay
Ā 
Java intro
Java introJava intro
Java intro
Ā 
Modules all the way down: OSGi and the Java Platform Module System
Modules all the way down: OSGi and the Java Platform Module SystemModules all the way down: OSGi and the Java Platform Module System
Modules all the way down: OSGi and the Java Platform Module System
Ā 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java Workshop
Ā 
Java 9 / Jigsaw - LJC / VJUG session (hackday session)
Java 9 / Jigsaw - LJC / VJUG session (hackday session)Java 9 / Jigsaw - LJC / VJUG session (hackday session)
Java 9 / Jigsaw - LJC / VJUG session (hackday session)
Ā 
Java 9 Module System
Java 9 Module SystemJava 9 Module System
Java 9 Module System
Ā 
Java 9
Java 9Java 9
Java 9
Ā 
Apache Maven supports ALL Java (Javaland 2019)
Apache Maven supports ALL Java (Javaland 2019)Apache Maven supports ALL Java (Javaland 2019)
Apache Maven supports ALL Java (Javaland 2019)
Ā 
What's New in Java 9
What's New in Java 9What's New in Java 9
What's New in Java 9
Ā 
Java course-in-mumbai
Java course-in-mumbaiJava course-in-mumbai
Java course-in-mumbai
Ā 
Java basic introduction
Java basic introductionJava basic introduction
Java basic introduction
Ā 
Apache Maven supports all Java (JokerConf 2018)
Apache Maven supports all Java (JokerConf 2018)Apache Maven supports all Java (JokerConf 2018)
Apache Maven supports all Java (JokerConf 2018)
Ā 
What's Expected in Java 7
What's Expected in Java 7What's Expected in Java 7
What's Expected in Java 7
Ā 
Preparing for java 9 modules upload
Preparing for java 9 modules uploadPreparing for java 9 modules upload
Preparing for java 9 modules upload
Ā 

Recently uploaded

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
Ā 
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļøcall girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļøDelhi Call girls
Ā 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfWilly Marroquin (WillyDevNET)
Ā 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
Ā 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
Ā 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
Ā 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
Ā 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
Ā 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
Ā 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
Ā 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanā€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanā€™s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanā€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanā€™s ...OnePlan Solutions
Ā 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
Ā 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
Ā 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
Ā 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
Ā 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
Ā 
Introducing Microsoftā€™s new Enterprise Work Management (EWM) Solution
Introducing Microsoftā€™s new Enterprise Work Management (EWM) SolutionIntroducing Microsoftā€™s new Enterprise Work Management (EWM) Solution
Introducing Microsoftā€™s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
Ā 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
Ā 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
Ā 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
Ā 

Recently uploaded (20)

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
Ā 
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļøcall girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
Ā 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
Ā 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
Ā 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
Ā 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
Ā 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
Ā 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
Ā 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
Ā 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
Ā 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanā€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanā€™s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanā€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanā€™s ...
Ā 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
Ā 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
Ā 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
Ā 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
Ā 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
Ā 
Introducing Microsoftā€™s new Enterprise Work Management (EWM) Solution
Introducing Microsoftā€™s new Enterprise Work Management (EWM) SolutionIntroducing Microsoftā€™s new Enterprise Work Management (EWM) Solution
Introducing Microsoftā€™s new Enterprise Work Management (EWM) Solution
Ā 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
Ā 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Ā 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
Ā 

Java 9 New Features

  • 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