3. JShell (Java Shell)
❏ JShell is the read-eval-print-loop (REPL) tool used for interacting quickly with java from
command line platform without the need to compile and run.
❏ Configure external editor with jshell.(/set editor “path” -w)
❏ Working with JAR/Module and file by setting classpath. (jshell --class-path “location”)
❏ It’s great for investigation of APIs that are unfamiliar to us.
❏ It’s wonderful for quick prototyping of tricky code. Like RegeX, Calculation based etc...
❏ Reference link:
❏ Link1
❏ Link2
4. JLink (Java Linker)
❏ Used for creating our own customize JRE
❏ Benefit:
❏ Smaller size of JAR.
❏ Suitable for IOT device and Microservices
❏ Running a simple JAR from anywhere even not required to pass “java” prefix.
5. Change in Interface
❏ Until now (Java 8), we can have only default, static and abstract method inside interface.
❏ From Java 9, we can have private method as well.
❏ Benefit:
❏ To avoid redundant code and more reusability
6. Immutable Collections
❏ Factory method for creating unmodifiable collections.
❏ Limitation:
❏ Can create only 0 to 10 size.
❏ But One method with varargs that let us create with desired number of element
❏ Benefit:
❏ Being shorter and nice to read
❏ List<String> string = List.of(“1” , ”2”);
❏ Set<Integer> set = Set.of(1,2);
❏ Map<Integer,Integer> map = Map.of(1,”a”,2,”b”);
❏ If we try to create with null element, a NullPointerException will be thrown.
❏ List<String> mutable = new ArrayList<>(List.of(“1”,”2”); mutable.add(null);
7. try-with-resource Improvement
❏ Till Java 8
void readFile() throw IOException{
BufferedReader reader1 = new
BufferedReader (new FileReader(“abc.txt”);
try(BufferedReader reader2 =reader){
System.out.println(reader2.readLine());
}
}
❏ Java 9
void readFile() throw IOException{
BufferedReader reader1 = new
BufferedReader (new FileReader(“abc.txt”);
try(reader1){
System.out.println(reader1.readLine());
}
}
8. Deprecated Enhancements
❏ Deprecated(since=”1.5” forRemoval=true)
❏ forRemoval:
❏ true means that “this API element is earmarked for removal in future
release”If they don’t migrate away from the APU, their code could be
break when new released is issued.
❏ false indicate that the API element is deprecated, but without any
specific intent to remove that API in the future.
❏ The default value for this element is false.
❏ since:
❏ The String should contain the release or version number at which this API
became deprecated.
❏ The default value of this element is empty string.
9. JPMS (Java Platform Module System)
❏ What is JPMS ?
❏ Why we need JPMS ?
❏ How JPMS will solve existing problem ?
Well know as Jigsaw project.
10. What is JPMS ?
❏ The Java Platform Module System is a self-describing collection of Code, Data and
some Resources. It is a set of related Packages, Types (classes, abstract classes,
interfaces etc) with Code & Data and Resources.
Any Confusion?
11. ❏ If the idea of the module construct in java is simply to break code in
small encapsulated unit,then we could do it before java 9.
Question?
❏ Do you think, still we need Modularized JDK ?
❏ How will you control over class, package, jar etc...?
❏ What are the problem faced by developer in real life. ?
12. Problem with JAR or ClassPath (JAR HELL)
❏ NoClassDefFoundError in middle of program execution.
❏ Version Conflict.
❏ Security threat .
❏ Bigger size, Unable to fit in IOT device and Microservices.
13. Why JPMS ?
❏ Reliable configuration
❏ To provide better encapsulation, because public was too much public.
❏ To provide security
❏ Scalable java platform (in small memory we can run java application)
❏ Performance and Memory Improvement
❏ Question? What is special in java 9
14. How JPMS will solve existing problem ?
❏ Whole JDK is modularized.
❏ All predefined java class are divided in 98 module.
❏ Module contains configuration information inside a file “module-info.java”
❏ System.out.println(String.class.getModule());
15. Type of module
❏ There are three type of module.
❏ Named Module
❏ Unnamed module
❏ Automatic module
16. Type of module
❏ Named module
❏ A named module is a module created with a module declaration file module-info.java in its root
folder.
module module_name {
requires other_module_1;
exports package_name_1;
exports package_name to other_module; //
qualified export
opens some.client.package to framework.module;
}
17. Type of module
❏ Unnamed module
❏ Unnamed module is a jar built without module-info.java declaration. This means all
current jars built in Java 8 and earlier releases are unnamed modules.
❏ JVM choose unnamed module name always.
❏ What modules it 'requires'?
● The unnamed module 'requires' every other named modules automatically.
● That means all classes within the unnamed module can read all other module (named or
unnamed) without any explicit declaration of any kind.
● That also means that older classes (written prior to Java 9) can read all modules defined by
the new Module system.
18. Type of module
❏ Unnamed module
❏ What packages it 'exports'?
● The unnamed module 'exports' all its packages.
● That means different Jar applications which do not contain module or which are
compiled in the older versions, will continue to use each other's dependencies as it is.
● The packages exported by unnamed module can only be read by another unnamed
module.
❏ Do you have any question? Why unnamed module export all packages?
● This only ensure other unnamed modules can access its packages.
21. Unnamed module vs class path/module path
❏ Running using module path
java --module-path out -module moduleName/com.netkumar.code.one.Unnamed
What are you going to use moduleName above?
We can’t run unnamed module name on module path.
❏ why don't we see all named modules of JSE 9 in above output.
● Java 9 import module on demand
22. Using --describe-module
❏ --describe-module <module name> is a java command option,which describes a module details.
❏ jar command also has --describe-module option which does not require a module name. It describes the
module in the specified jar.
23. Summary
● The unnamed module does not have module-info.java.
● The unnamed module 'requires' all other modules and 'exports' all its packages.
● The classes which were written in the older versions but now running in Java 9 environment will
continue to work because they become the members of the unnamed module.
● If we want to run old code in Java 9 environment without migrating them to the module system, we
should run them using class path (not module path)
24. Root Module
Let’s understand root module first.
What is 'requires transitive'
❏ Some modules which contain only module-info.java and no packages or no Java code. Their sole
purpose is to require other modules (called root modules) and make them visible outside. 'java.se' is
one of such module.
❏ 'requires transitive java.logging' will make java.logging available to the module which requires java.se
module.
❏ requires transitive == export or requires transitive != export ? what do you think about this ?
● 'exports' is used to make packages visible,
● 'requires transitive' is used to make imported modules visible outside.
25. Root Module
How root modules are used?
❏ When the Unnamed Module is being compiled or loaded, one of the set of root modules should be
accessible to the Unnamed module (so that a non-modular application will continue to work).
26. Type of module
What if a named module wants to read the existing jars or third party jar built without module-info.java
created?
Automatic module
❏ Java will create module from jar files automatically. These module are called Automatica module.
❏ The only thing you need to do is to put the jar file in the module path instead of the class path.
❏ Once a jar without module-info is put in the module path, it becomes an automatic module
automatically.
❏ An automatic module can read all other modules and all other modules can read it as well. Even the
named modules can read an automatic module.
❏ How automatic module will have a module name derived from its jar name? Do we have any rule ?
27. Automatic module
Rules:
● It drop the jar file extension
● It replace - character with .
● It remove the version string
What packages it 'exports'?
❏ It 'exports' all its packages.
What modules it 'requires'?
❏ It 'requires' all other modules on the module path.
30. Live coding for below scenario
❏ Compatibility and Migration
❏ Split Packages
❏ Module Modes
❏ Modules and Services
❏ Qualified Exports
❏ Reflective Access
CLI Compilation: (Note:- share cheat sheet)
Javac --module-source-path “give module source path ” -d “where you want to
create out dir” “all java file or module name”
Java --module-path “out” --add-module “module name” “give main class”
31. Diamond operator ->Anonymous Inner
class❏ We can create <> operator for anonymous inner classes from JDK9.
Example:
List<String> list = new ArrayList<String>(){
{
add(“one”);
add(“two”);
}
}
32. SafeVarargs Enhancement
❏ We can apply @safevarargs annotation on private method from jdk9.
Example:
@safeVarargs
private static String getInfo(){
//do some file or db work.
}
33. Stream API Enhancement
❏ Jdk 9 added four new method for stream API.
❏ takeWhile().
❏ dropWhile().
❏ iterate().
❏ ofNullable().
34. Http/2 Client
❏ HTTP/1.1 is blocking mode, lot of performance issue and not supported HTTP2.
❏ HTTP/2 can send multiple request for data in parallel over a single TCP connection and
ASync mode.
❏ There are 3 new classes introduced to handle HTTP communication.
1) HttpClient (HttpClient handles the creation and send of requests.)
2) HttpRequest (HttpRequest is used to construct a request to be sent via the HttpClient.)
3) HttpResponse (HttpResponse holds the response from the request that has been sent.)
Q-> What does means of “incubator”?
35. Process API updates
❏ Two new interface has been added to JDK9, for providing more control over os
process.
❏ ProcessHandle
❏ ProcessHandle.Info
Example: ProcessHandle ph = ProcessHandle.current();
ph.getEveryThinh…..
36. Others Change
Compact String Enhancement->
Q-> What is the problem in existing String class ?
Q-> How String class engage our GC ? >>>Reference Link
Garbage Collector ->
G1 will be Default GC from JDK 9
Logging Updates ->
>>>Reference Link
Optional API Updated->
>>>Reference Link
37. Others Change
Array Utility ->
1)equals – returns true if two arrays are equal to each other.
2)compare – compares two arrays lexicographically (similar to dictionary order).
3)mismatch – finds and returns the index of the first mismatch between two arrays.
Reference Link
Stack-Walking API – Java 9->
Reference Link
38. Reactive Stream
❏ What is Reactive Programming ?
❏ Why Reactive Programming ?
❏ Reactive programming != Reactive system. (any confusion ?)
❏ The four Reactive principles
❏ JDK 9 Flow API.
❏ Non-blocking Back Pressure
❏ Reactive low level architecture.
❏ Callback Hell, How reactive solves the problem ?
❏ RXJava Implementation for java
39. Reactive Stream
❏ Java 9 includes a publish-subscribe framework for reactive streams.
❏ Flow is a repository for four nested static interfaces whose methods establish flow-
controlled components in which publishers produce data items that are consumed by one or
more subscribers:
❏ Publisher: A producer of data items that are received by subscribers.
❏ Subscriber: A receiver of data items.
❏ Subscription: Linkage between a Publisher and a Subscriber.
❏ Processor: A combination of Publisher and Subscriber for specifying a data-
transformation function.
40. Subscriber Interface
public static interface Subscriber<T> {
public void onSubscribe(Subscription subscription);
public void onNext(T item);
public void onError(Throwable throwable);
public void onComplete();
}
41. Let’s see all four concrete method impl
onSubscribe: invoked after a Publisher has completed the subscription for this
Subscriber (but before sending any Subscription item). The newly created
Subscription object is passed via this method. The Subscriber typically assigns this
instance to an instance variable for further use.
onNext: invoked with a Subscription's next item of type T.
onError: invoked upon an unrecoverable error encountered by a Publisher or
Subscription.
onComplete: invoked when no additional Subscriber method invocations will
occur including onNext() method.
42. Subscription Interface
❏ This is the key method behind non-blocking back-pressure concept. The
Subscriber uses it to request n more items for consumption. This way the
Subscriber controls how many items it is currently capable to receive (probably
it will want to limit the consumption according to what resources it has).
❏ Used by the Subscriber to cancel its subscription. After this call, no further
items will be received.
44. Processor Interface
A component that acts as both a Subscriber and Publisher.
public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> {
}
45. SubmissionPublisher class
❏ This is the only concrete class provided in the Reactive Streams API. It
implements the Publisher interface. We can use its submit() method to publish
the provided item to each subscriber. In the following example we will see how
to use this class and at the same time we will get familiar with the usage of
above interfaces.