Friday 27 July 2018

Java 9 - New features

JShell : 
It's an editor where users can quickly test/learn the Java skills.
To test even System.out.println("Hello.."); --> we need to create class, public static void main(String st[]) etc....
We need to know what is Class/String/Object/System classes....
To avoid all these, Java has released JShell, it's an editor, it just allows user to enter print("hello....");
To start jshell, we just need to enter C:/> jshell PRINTING
This command opens editor jshell> 
Now user can just enter jshell>print("hello"); ==> This prints out hello
Can also calculate jshell> 10+20 ==> prints 30
Can also test the operators jshell> 10 > 20 ==> print false.


JPMS (Java Platform Module System):
From Java 9 onwards, JDK has become Modularized.
Till Java 8, Java is completely runs on Jar file approach.
These are few problems till Java 8 (with Jar file approach):
1) NoClassDefNotFoundError Exception
2) Version conflicts
3) Anybody can access jar files code (Security issues)
4) To execute 'hello world' code, we require 400 MB JRE (includes rt.jar) - its completely a monolithic approach
All these issues combinely called "Jar Hell" issue
From Java 9, there is no tools.jar/rt.jar, rt.jar is several modules.
Only required modules we can use at runtime, no need to load the entire rt.jar

JLINK (Java Linker):
It's a tool create our own customized JRE with small size. No need to load complete rt.jar/tools.jar
Till Java8, to execute 'hello world' code, we require 400 MB JRE (includes rt.jar/tools.jar).
By using Java9, we can use only required classes (Class/String/Object/System classes....) we can run "Hello world" code, and create our own JRE.
Once we get our own JRE, we can deploy that JRE and run our small JRE on IOT devices/Micro Chips etc.
To create our own JRE use this command : $ jlink --module-path <modulepath> --add-modules <modules> --limit-modules <modules> --output <path>

HTTP/2 Client:
To send and receive HTTP request/responses we need use Browsers. 
To send HTTP request from Java application we need to use HTTP Client.
Problems with current HTTP class till Java 8:
1) To send HTTP request  we use HttpURLConnection, but this is released in 1997. 
As per the current standards we are still using legacy classes, it's not good for the performances.
2) At a time only one request we can send per TCP/IP connection, if we try to send more requests performance will automatically come down.
3) Supports only for text, not for Binary data.
4) Always works in Blocking mode, Synchronous mode, until we get response client will be in a blocking mode.
HTTP/2 client can fix all the above issues.
1) Can send multiple requests
2) Supports Binary/Text data
3) It supports Synchronous/Asynchronous modes

Process API Updates:
Until Java 8, it's difficult to communication with Processor is very difficult.
To get Processor level data, we need to write huge amount of code. 
With a single line of code we can get Process ID.
To get process id, System.out.println(ProcessHandle.current().pid()); ==> ProcessHandle is included in java.lang package itself. Not required of any imports.
From Java 9 onwards communication with Processor level as become very easy.

Private methods inside 'interfaces' :
This features has been introduced to make a reusable methods inside interface itself.
Until Java 8, we have - default and static methods in interface. 
If there is any common code with in multiple default methods, we can keep those common code inside private method.
Without effecting implementation classes if we want code re-usability we need to go for private methods. 
So that it's not going to impact the implemented classes.

'try' with resources :
Till Java 8, we can use syntax like : 
try(FileWriter fw = new FileWriter("writer.txt"); FileReader fr = new FileReader("reader.txt")){
}

From Java 9 ,we can declare them outside and use references with in try(...).
FileWriter fw = new FileWriter("writer.txt"); 
FileReader fr = new FileReader("reader.txt")
try(fw; fr){
}

Factory methods to create Unmodified collections:
Until Java 8:
List<String> li = new ArrayList<>();
li.add("A");li.add("B");li.add("C");
List<String> unmodif = Collections.unModifiableList(li);

Java 9:
List<String> li = List.of("A","B","C");

Stream  API Enhancements:
Stream we use to process the collection objects.
Few methods added in Java 9:
1) takeWhile() --> Object method
2) dropWhile() --> Object method
3) Stream.iterate() --> static method
4) Stream.ofNullable() --> To check the null values --> static method

<> operator:
Until Java 8, we can declare <> operator for all Classes.
From Java 9, we can use it for Anonymous inner classes: 
example:
List<String> al = new ArrayList<>(){
//This is an annonymous inner class.
};

SafeVarargs Annotation:
Until Java 8:
We can use this annotation for static methods/ final methods/ constructor
From Java 9: 
We can use for private methods also.

Until Java 8:
method(String... st);
methodL(List<String>... li){
// This method will generate "Heap Pollution" WARNINGS.


From Java 9:
If we are sure that Heap Pollution doesn't occur, we can suppress these warnings using an Annotation.
@SafeVarargs
methodL(List<String>... li){
//No warnings now.


G1GC(Garbage First Garbage Collector):
Different Types of Garbage Collectors:
Serial GC
Parallel GC
Concurrent Mark Sweep(CMS) GC
G1 GC -> is introduced in Java 6 itself.

Until Java 8 : 
Default Garbage Collector is 'Parallel GC'

From Java 9:
G1 GC has become the default GarbageCollector. 
Total Heap is divided into multiple regions and G1GC always chooses region which are having highest number of objects eligible for GC, then it clears that region.

1 comment:

  1. This blog gives very important info about AWS,Thanks for sharing
    Nice Blog
    AWS Online Course

    ReplyDelete