Java SE 9 - SHRINATH JOSHI
Java 9  Java 9 was released on 21st September 2017  The main goals for Java 9 are to:  Make the Java Standard Edition platform, and the JDK, more navigable to scale down for small computing devices.  Improve the overall security and maintain not only the JDK but the Java Implementations in general.  Allow overall improved application performance.  Make it easier for Java developers to build and uphold the code libraries and larger applications, for both the Java SE and EE Platforms.
Java 9 Features  Jshell  JPMS (Java Platform Module System)  Jlink (Java Linker)  Http/2 Client API  Process API Update  Private method inside interface
Java 9 Features cont..  Try with resources enhancement  Factory method to create unmodifiadble collections  Stream Api enhancements  < > (Diamond )operator Enhancement  SafeVargs annotations  G1GC (Garbage First Garbage collector)
JShell  Like a playground for Begineers where individual can start learning without writing the boilerplate code  The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code.  JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.  The tool is run from the command line.
JPMS  Until java 8 ,while developing software we first define the classes, interfaces,enum and put this inside package and then this packages is kept as Jar file.  This has few disadvantages eg NoClassDefFoundError, VersionConflict, Security Problem.  Untill Java 8, Jdk and Jre had monolithic structure i.e. it cannot be divided. To execute a simple hello World program of 1 kb the client had to load 400Mb of JRE on client machine.  All this problem is also known as Java Hell.  To overcome this problem JPMS comes into picture
 In modular programming the Jvm checks in the start of execution where all the required files are present or no .Therefore there is no Chance of NoclasssDefFoundError.  Part Of Jigsaw Project  Project started in 2005
JLink  To run a simple hello World program we need JRE . JRE contains rt.jar whose size is 60Mb . This rt.jar contains all the predefined class i.e it contains 4300+ classes but we required hardly 5-6 classes . The size of Jre is 400Mb . Therefore to run 1 kb program 400mb is required . Therefore java is heavy weight.  To create our own customised jre we use jLink  We can now use java for Iot deices,micro-services etc.
HTTP/2 Client  To send an Http request to server from a java program we need some software and that is http/2 clinet.  Until java 8 we used httpUrlConnection but this had certain drawback  httpUrlconnection was developed in 1997 ,introduced in java 1.0 and was still being used until java 8.  httpUrlconnection was difficult to use  Only one request at a time.  It supported only data in text format and not binary data.  It works in blocking mode i.e wait for the next request.
 Therefore Java programmer started using Apache Http client or google Httpclient.  Now in Java 9 they introduced Http/2 client  Very easy to use  Works in both blocking and non blocking mode  Works with data in both text and binary format  Can send multiple requests.
Process API Updates  Java communication with processor is very difficult until java 8, but from java 9 new methods added to Process class i.e Api got enhanced.  Most imp feature of java 9  System.out.println(ProcessHandle.current().pid()); to get the current running jvm process
Private method inside Interface  Until 1.7 inside interface wecan take only public and abstract methods and every variable present inside interface is public static and final  From 1.8 we can take default methods and static methods  From 1.9 we can take private methods inside interface  Without affecting the implementation classes we can extends the functionality of interface by adding default methods  Interface inter  {  M1();  M2();  }
 Class a implements inter  {  M1() {}  M2() {}  }  .  .  .  Class class10 implents inter  {  M1(){}  M2(){}  }
 To add a method in interface we have to make changes in all the implanting classes .therefor we add default method  Interface infer  {  M1(){}  M2(){}  Default void m3(){}  }  Implementing classes are not required to override default methods.
 Private methods in interface are introduced to make code reuseable  Without affecting implementation class how we can get Code Reusablilty?.  Consider the below code  Since the method is private it is hidden from the implementing class  This make code reusable .
 Interface interface  {  Defaulf void m1()  {  m3();  }  Default void m2()  {  m3();  }  Private void m3()  {  Common code;  }  }
Try with Resources enhancement  Try with resources  Until 1.6 , if we have to close the resources we wrote that code in finally block  From 1.7 try with resources came  try(r1;r2)  {  }  These resources were closed automatically after the try block.
 Fw fw=new Fw(“abc.txt”);  Fr fr=new Fr(“input.txt”);  Try(fw;fr)  {  }// Not allowed in Java 8  // We have to declare inside try()  From Java 9 we can use try with resources where resources are already being created
Factory Methods to create Unmodifiable Collections  List <String> l =new ArrayList<>();  L.add(“A”);  L.add(“B”);  L.add(“C”);  Now if I have to make list immutable??  Until Java 8  List <String > l2=Collections.unmodifiableList(l);  From Java 9  List<String> l=List.of(“A”,”B”,”C”);  Set.of();
Stream API Enhancement  Collection is a group of objects.  The main objective of collection is to hold objects as a single entitiy and Stream is used to process this object from the collections.  New Methods in Java 9  takeWhile() //Default Methods in Stream Interface  dropWhile() -------- -------  Stream.iterate() with three argument  Stream.ofNullable() //to check if null reference
<> Operator  ArrayList <String> l =new ArrayList<String>();  Generics is used to perform typesafety and perform and resolve type casting problems  From Java 7  ArrayList <String> l =new ArrayList<>(); // No need to specify the type parameter  Also know as type inference  This <>operator is only applicable for normal classes and not applicable for Anonymous Inner Classes  From Java 9 we can use <>(diamond) operator in anonymous inner classes.
safeVarargs Annotation  In Java 5 m1(int … x)  M1(List<String>… x)  Here there may be a chance of heap Pollution.  // Warning by compiler  If we want to supress this warning we can go for safevarargs annotations  This concept came in java 8 wherein safevarargs is used for static ,final ,constructor methods  From Java 9 ,it is also used for private methods also
G1GC Garbage First Garbage collection  Many type of GC  Serial Gc  Parallel GC  Concurrent mark and Sweep gc  G1gc //from 1.6  First three will work based on generations  G1gc divides the heap into parts and uses the part with most garbage i.e highest number of objects that are eligible for gc and will destroy that area  Performance wise g1gc is best  From Java 9 ,g1gc is default garbage collector  Untill java 8 , parallel gc was default garbage collector
Thank You

Java 9 features

  • 1.
    Java SE 9 -SHRINATH JOSHI
  • 2.
    Java 9  Java9 was released on 21st September 2017  The main goals for Java 9 are to:  Make the Java Standard Edition platform, and the JDK, more navigable to scale down for small computing devices.  Improve the overall security and maintain not only the JDK but the Java Implementations in general.  Allow overall improved application performance.  Make it easier for Java developers to build and uphold the code libraries and larger applications, for both the Java SE and EE Platforms.
  • 3.
    Java 9 Features Jshell  JPMS (Java Platform Module System)  Jlink (Java Linker)  Http/2 Client API  Process API Update  Private method inside interface
  • 4.
    Java 9 Featurescont..  Try with resources enhancement  Factory method to create unmodifiadble collections  Stream Api enhancements  < > (Diamond )operator Enhancement  SafeVargs annotations  G1GC (Garbage First Garbage collector)
  • 5.
    JShell  Like aplayground for Begineers where individual can start learning without writing the boilerplate code  The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code.  JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.  The tool is run from the command line.
  • 6.
    JPMS  Until java8 ,while developing software we first define the classes, interfaces,enum and put this inside package and then this packages is kept as Jar file.  This has few disadvantages eg NoClassDefFoundError, VersionConflict, Security Problem.  Untill Java 8, Jdk and Jre had monolithic structure i.e. it cannot be divided. To execute a simple hello World program of 1 kb the client had to load 400Mb of JRE on client machine.  All this problem is also known as Java Hell.  To overcome this problem JPMS comes into picture
  • 7.
     In modularprogramming the Jvm checks in the start of execution where all the required files are present or no .Therefore there is no Chance of NoclasssDefFoundError.  Part Of Jigsaw Project  Project started in 2005
  • 8.
    JLink  To runa simple hello World program we need JRE . JRE contains rt.jar whose size is 60Mb . This rt.jar contains all the predefined class i.e it contains 4300+ classes but we required hardly 5-6 classes . The size of Jre is 400Mb . Therefore to run 1 kb program 400mb is required . Therefore java is heavy weight.  To create our own customised jre we use jLink  We can now use java for Iot deices,micro-services etc.
  • 9.
    HTTP/2 Client  Tosend an Http request to server from a java program we need some software and that is http/2 clinet.  Until java 8 we used httpUrlConnection but this had certain drawback  httpUrlconnection was developed in 1997 ,introduced in java 1.0 and was still being used until java 8.  httpUrlconnection was difficult to use  Only one request at a time.  It supported only data in text format and not binary data.  It works in blocking mode i.e wait for the next request.
  • 10.
     Therefore Javaprogrammer started using Apache Http client or google Httpclient.  Now in Java 9 they introduced Http/2 client  Very easy to use  Works in both blocking and non blocking mode  Works with data in both text and binary format  Can send multiple requests.
  • 11.
    Process API Updates Java communication with processor is very difficult until java 8, but from java 9 new methods added to Process class i.e Api got enhanced.  Most imp feature of java 9  System.out.println(ProcessHandle.current().pid()); to get the current running jvm process
  • 12.
    Private method insideInterface  Until 1.7 inside interface wecan take only public and abstract methods and every variable present inside interface is public static and final  From 1.8 we can take default methods and static methods  From 1.9 we can take private methods inside interface  Without affecting the implementation classes we can extends the functionality of interface by adding default methods  Interface inter  {  M1();  M2();  }
  • 13.
     Class aimplements inter  {  M1() {}  M2() {}  }  .  .  .  Class class10 implents inter  {  M1(){}  M2(){}  }
  • 14.
     To adda method in interface we have to make changes in all the implanting classes .therefor we add default method  Interface infer  {  M1(){}  M2(){}  Default void m3(){}  }  Implementing classes are not required to override default methods.
  • 15.
     Private methodsin interface are introduced to make code reuseable  Without affecting implementation class how we can get Code Reusablilty?.  Consider the below code  Since the method is private it is hidden from the implementing class  This make code reusable .
  • 16.
     Interface interface {  Defaulf void m1()  {  m3();  }  Default void m2()  {  m3();  }  Private void m3()  {  Common code;  }  }
  • 17.
    Try with Resourcesenhancement  Try with resources  Until 1.6 , if we have to close the resources we wrote that code in finally block  From 1.7 try with resources came  try(r1;r2)  {  }  These resources were closed automatically after the try block.
  • 18.
     Fw fw=newFw(“abc.txt”);  Fr fr=new Fr(“input.txt”);  Try(fw;fr)  {  }// Not allowed in Java 8  // We have to declare inside try()  From Java 9 we can use try with resources where resources are already being created
  • 19.
    Factory Methods tocreate Unmodifiable Collections  List <String> l =new ArrayList<>();  L.add(“A”);  L.add(“B”);  L.add(“C”);  Now if I have to make list immutable??  Until Java 8  List <String > l2=Collections.unmodifiableList(l);  From Java 9  List<String> l=List.of(“A”,”B”,”C”);  Set.of();
  • 20.
    Stream API Enhancement Collection is a group of objects.  The main objective of collection is to hold objects as a single entitiy and Stream is used to process this object from the collections.  New Methods in Java 9  takeWhile() //Default Methods in Stream Interface  dropWhile() -------- -------  Stream.iterate() with three argument  Stream.ofNullable() //to check if null reference
  • 21.
    <> Operator  ArrayList<String> l =new ArrayList<String>();  Generics is used to perform typesafety and perform and resolve type casting problems  From Java 7  ArrayList <String> l =new ArrayList<>(); // No need to specify the type parameter  Also know as type inference  This <>operator is only applicable for normal classes and not applicable for Anonymous Inner Classes  From Java 9 we can use <>(diamond) operator in anonymous inner classes.
  • 22.
    safeVarargs Annotation  InJava 5 m1(int … x)  M1(List<String>… x)  Here there may be a chance of heap Pollution.  // Warning by compiler  If we want to supress this warning we can go for safevarargs annotations  This concept came in java 8 wherein safevarargs is used for static ,final ,constructor methods  From Java 9 ,it is also used for private methods also
  • 23.
    G1GC Garbage First Garbagecollection  Many type of GC  Serial Gc  Parallel GC  Concurrent mark and Sweep gc  G1gc //from 1.6  First three will work based on generations  G1gc divides the heap into parts and uses the part with most garbage i.e highest number of objects that are eligible for gc and will destroy that area  Performance wise g1gc is best  From Java 9 ,g1gc is default garbage collector  Untill java 8 , parallel gc was default garbage collector
  • 24.