Java 9
JShell
JavaPlatformModuleSystem(JPMS)
JLink (java linker)
HTTP/2 Client
Process Api Updates( creating the process, kill the process)
private methods in insides interface
try with resources enhancement
Factory Method to create unmodified collections
StreamApi enhancements
Diamond Operator enhancements
Safe vargs annotation
G1GC came on 1.6
1.Jshell:
The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code.
Java's Repl(Read evaluate print loop) tool for interactive shell .
This is useful for Beginner to learn and also developers(To test there coding snippets)
So How it useful for developer let's say i want to know the output of the Math.sqrt(number) this function so if we write all the class methods without this, we can directly check by jshell.
We are not required to install Jshell it comes up with Java 9 we install Java 9 if we want to start jshell then go to command prompt and enter Jshell then Jshell will start or enter jshell -v (v is verbose mode in this some extra information will displays).
To exit from the Jshell> /exit.
Every command will start with /
In Jshell semicolon is not required to worry Jshell takecare of it.
10 Default Packages are imported in Jshell. How we will know just enter this command
/imports
2.JPMS:
Before Java 1.8 we use Class+interfce+ enum put into the packages and convert into the jar
jar is a group of packages.
In jar we have some problems to overcomes this we have module.
Module is group of packages which contains the configurable information inside a special file that is module-info.java
before java 1.8 we have rt.jar(which has all the classes) after java 9 rt.jar is convert into the 98 modules each module contains serval packages and a special file.
So In jar file mode if one jar file want requires another jar files this configuration is snot specifies in jar mode But in that Module this configuration information are present in the module-info.java
At the beginning of jvm start then it check all dependency from java 9 In java 8 it will check on runtime if not found it gets class not found exception
from java 9 security related problem i will not come for jar by default every jar is expose to outside but from 9 we can set not to export the jar outside which are sensitive.
To run our application we need rt.jar it have all the classes but we want onlu 4 classs then we have to import all these class it a heavy weight from java 9 we can import the modules what we want .
3.JLINK( java linker):
We can create your own JRE . suppose if want to run a program of 1 kb we need the jre of 203mb by default so to overcome this we need to create your own jre.
4.Try With Resource Enhancements:
this comes up in java 1.7 version.
Until 1.6 we use finally block to close up the resources. so programmer has to close the resource so it leads to complexity and code increases
In 1.7 try with resource so this will take care of the closing the resources programmee will not take care of closing the resource.
So we have to create resource in the try block in 1.7 and from 1.9 we can give reference to resource in the try block we don't need to create the resouce in try block and we can pass multiple resources in the try block.
5.Private Methods In interface:
until 1.7 version we can write public abstract method and static final variables after 1.8 we can have deault methods and static methods. in 1.9 private methods are allowed
6.DiamondOperator enhancement:
Generics-1.5v ,Diamond Operator -1.7v
Why generics to provide the type safe and to resolve type casting and we have to specify the typeparamter in that ex: ArrayList<String> a=new ArrayList<String>();
Diamond Operator(<>) in 1.7 we can write code without specifyin th etyep parameter f
ex:ArrayList<String> a=new ArrayList<>();
after 1.9 we can use diamond operator for anonymous clasess
ArrayList<String> a=new ArrayList<>(){
}
7.HTTP/2:
to send a requet from the java In java 9 This concept came Http/2 client before 1.9 we use HttpUrlConnection()(This came in 1.1 v 1997) why this Http/2 came cz to overcome HttpUrlConnection problems
Problems with HttpUrlConnection:
1. Supports for Http/1.1
8.Safe Args Annotations:
this came in java 7 and small enhancement comes in java 9
var-args come sin 1.5v it takes any no of parameter for only one method we use ellipse symbol-> ... and flexibility is good but performance is costly there is a degrade by default this var args converted into 1D array.
Inside JVM memory areas:
Inside Heap memory we store objects
Java 9 solves heap pollution area.(One type of variable is pointing to another type object) we will get classcastexception
@SafeVarargs -> 1.7v to supress warning related to var args methods and heap pollution problem and this use for constructors ,static methods and final methods.
After java 9 we can apply this for private methods also
9.Factory methods to create unmodified Collections:
factory method means when are using class name to get the instance which return same class object
If we want to create unmodified Collection for example
List<Integer> l=new ArrayList<>();
l.add(1) ;l.add(2)
l=Collections.unmodifiedList(l);
from java 9 just with one step List<Integer> l=List.of(1,2,3);
we can't use null if we use get nulpointerexceprction
if we use duplicate values in set then we get Illegal exception.
10.ProcessApi Updates:
i until i.8 v communicating with underling process is difficult the way of process handling is varying from one processor to another processor
1.we can get the id of the current running procees
2.we can create a process
destroy the process
Complete information of process
we can get child process and parent process information
Java 9 :
Process class is old class in that some method are introduced: that are pid(),info()
processBuilder
ProcessHadler
11.StreamApi Enhancements:
In java 9 four methods came
these methods are added:
takeWhile(predicate); ex i have list [8,6,1,2,3,4] if i use this method for check even or not then o/p is [8,6]
dropWhile() it is reverse of the takeWhile it will only print odd number if condition is even also
these two methods are default methods
Stream.iterate():
// Generate a stream of numbers starting from 1 and stop when number is greater than 10 Stream.iterate(1, n -> n <= 10, n -> n + 1) .forEach(System.out::println);
Stream.ofNullable():
Stream<T> ofNullable(T t)
If
t
is non-null
, it returns a stream containingt
.If
t
isnull
, it returns an empty stream.
these two methods are static methods