INTRODUCTION
we are learning java 11 features. what are java 11 features and how it helps developer with some new enhancement. So we are learning some important features of java 11.
What are java 11 features ?
1) Running java file :
In java 8 version we first compile the java file with javac command and then run the java file. But in java 11 we directly run the file in single line command.
JAVA 8
javac Myapplication.java
java Myapplication.java
JAVA 11
java Myapplication.java
2) Java New String Methods :
Java 11 introduced new string methods.
1) isBlank() :
Example :
2) lines() :
This method return stream of string divided by lines terminators.
Example :
3) repeat() :
This method used to repeat the string.
Example :
4) strip() , stripLeading() and stripTrailing() :
This methods are used to remove white space from the string. It is similar to method trim(). It also remove the white space from start and end of string.
Example :
3) New File methods :
In java 8 we use BufferReader() , FileReader, Scanner() to read the data from file, but java 11 made this easier. readString() and writeString() two static method added in File.
Example :
4) Collection toArray() method :
This toArray() method used to convert collection to array.
Example :
5) Path.of() :
In java 8, we need to create the Path object via Path.get() /File.toPath(). But since java 11 we can directly use Path object directly.
Example :
Path.of("C://user/root/document/")
6) HTTP Client :
Before java 11 , we need to write multiple code to get the response from service. We need to use BufferReader.line() , InputStreamReader() , But java 11 made it easy with single line.
Java 8 :
Java 11 :
so java help to remove multiple lines of code in http client.
7) Timeout Conversion :
This method is used to convert the given time to a unit like DAY, MONTH, YEAR, and for time.
Example :
Before java 11
long hourInMillis = TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS);
Java 11 :
long hourInMillis = TimeUnit.MILLISECONDS.convert(Duration.of(1,TimeUnit.HOURS.toChronoUnit()));
8) Epsilon garbage collector :
Java 11 introduced a no-operations(No-Op) garbage collector called Epsilon. This is an experimental feature. It is called a No-Op garbage collector because it will allocate memory but will never collect any garbage.
1) Performance testing
2) Memory pressure testing
3) VM interface testing
Use below command to enable epsilon garbage collector
-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC
10) Dynamic class file constant :
In Java 11, the Java Class-File format supports a new constant pool form called the CONSTANT_Dynamic. This will delegate creation to a bootstrap method. This was introduced to reduce the cost of creating new forms of materializable class-file constants by creating a single new constant-pool form that will be parameterized with appropriate user-defined behavior.
SUMMARY
we have seen lots of java 11 features. String new methods, file new methods and we can use http method using new httpClient. Also timeout conversion.
0 Comments
Post a Comment