Java 11 is a new feature which was released only recently in the Java SE Platform. Every mid-year, Oracle releases a new Java Version, and in September 2018, Java 11 created a new buzz. Through this release, Oracle made public that the licensing and support have been changed so downloading Java 11, Oracle JDK will make you pay for its commercial use. However, there are still ways to download the free version of Java.
What are some of the critical Java 11 Features for developers?
Java 11 New Features:
- Running Java File with a single command
Java 11 has brought in a major change one of which is that compiling the java source file with javac tool is not necessary at first. Now, java command can be used directly to run the file and you’ll see that it covertly compiles. This feature falls under JEP 330.
- Java String Methods
Following are some of the new methods of Java String class which has been introduced in the new Java 11:
- isBlank() – In this method, the empty Strings and strings with only white spaces are treated as blank. This string method returns a boolean value.
- lines() – It returns a string array that is an assembly of all substrings split by lines.
- strip(), stripLeading(), stripTrailing() – strip() removes the white space from both the beginning and the end of string. It is a Unicode-aware evolution of trim(). The new strip() removes all the white spaces leading and trailing. stripLeading() is used to remove white spaces in front of the string and stripTrailing() is used to remove white spaces in the back of the string.
- repeat(int) – It repeats the string as many times as mentioned in the method in the form of an int.
- Local-Variable Syntax for Lambda Parameters
The Java 10 with its Local Variable Type Inference made it possible to conclude this variable from the RHS – var list = new ArrayList<String>();
The JEP 323, Local-Variable Syntax for the Lambda Parameters is the only language feature release in the Java 11 version. However, JEP 323 allows var to be used to declare the formal parameters of an implicitly typed lambda expression.
Despite having the option of skipping this type in the Lambada, there is a need of this because when you need to apply an annotation just as @Nullable, you cannot do that without defining the type.
Some of the known limitations of these features are that you need to specify the type var on all parameters or none and there is no skipping or mixing allowed. Parentheses are needed if you use var in lambada.
- Nested Based Access Control
Before Java 11 was introduced, the private method of the main class was accessible from the nested class However, in the use of Java Reflection, it will give an IllegalStateException.
Thus, Java 11 nested access control has addressed this issue in reflection.
Three menthods have been introduced in the java.lang.Class which are getNestHost(), getNestMembers(), and isNestmateOf().
- Pattern recognizing methods:
The asMatchPredicate() method which is like the Java 8 method asPredicate() has been introduced in the Java 11 to create a predicate if the pattern matches with the input string.
- New File Methods:
There are three methods in new Java 11:
- writeString() which is used to write some content in a file
- readString() which is used to read the contents of a file
- isSameFile() which is used to check if two paths locate the same file or not
- Optional.isEmpty() Method:
It returns true if the value of any object is null and else returns false.
- JEP 309: Dynamic Class-File Constants
The Java 11 has now extended support from the Java class-file format to a new constant pool form, CONSTANT_Dynamic. This JEP aims to decrease the cost and disturbance of developing new forms of materializable class-file constraints. It does this by creating a single new constant-pool form which is parameterized with user-provided behavior and also improves performance.
- JEP 318: Epsilon: A No-Op Garbage Collector
Earlier the JVM GC was only responsible for allocating memory and releasing it. However, with the Epsilon, it is different, since it only allocates memory but does not have an actual memory reclamation mechanism. Following are the things that it allocates memory for:
- Performance testing.
- Memory pressure testing.
- VM interface testing.
- Extremely short-lived jobs.
- Last-drop latency improvements.
- Last-drop throughput improvements.
Now Elipson is good only for test environments. So once the Java heap is exhausted it will shut down the JVM. The reasons for using Elipson is that there is no memory clearance overhead so it’ll give an accurate test result of performance and you will not have to GC for stopping it which is a huge benefit.
- JEP 320: Remove the Java EE and CORBA Modules
In Java 9 these modules were deprecated with a declaration to remove those in future Java versions. Thus this has been completely removed in JDK 11. With JDK 11, the following packages have been removed: java.xml.ws, java.xml.bind, Java.activation, java.xml.ws.annotation, java.corba, Java.transaction, java.se.ee, jdk.xml.ws, jdk.xml.bind.
- JEP 328: Flight Recorder
Earlier the Flight Recorder used to be a commercial add-on in Oracle JDK. However, now it is open-sourced since Oracle JDK is itself not free anymore.
JFR which is a profiling tool to gather diagnostics and profiling data from a running Java application has a negligible performance overhead usually below 1% so is used in production applications.
- JEP 321: HTTP Client
Java 11 standardizes the Http CLient API which supports both HTTP/1.1 and HTTP/2. It enhances the overall performance of sending requests by a client and receiving responses from the server. HTTP Client also natively supports WebSockets.
- JEP 329: ChaCha20 and Poly1305 Cryptographic Algorithms
Java 11 provides ChaCha20 and ChaCha20-Poly1305 Cipher implementations which will be applied in the SunJCE provider.
- JEP 315: Improve Aarch64 Intrinsics
The existing string and array intrinsics have been improved and new intrinsics for the java.lang.Math sin, cos, and log functions, on AArch64 processors, have been implemented.
- JEP 333: ZGC: A Scalable Low-Latency GC (Experimental Feature)
A low latency Garbage Collector has been introduced in the new Java 11 and is an experimental feature.
Some of the features and options that have been removed in the new Java 11 are the com.sun.awt.AWTUtilities Class, Lucida Fonts from Oracle JDK, appletviewer Launcher, sun.misc.Unsafe.defineClass, Thread.destroy() and Thread.stop(Throwable) Methods, sun.nio.ch.disableSystemWideOverlappingFileLockCheck Property, sun.locale.formatasdefault Property, JVM-MANAGEMENT-MIB.mib, SNMP Agent, Java Deployment Technologies, JMC from the Oracle JDK, JavaFX from the Oracle JDK, and JEP 320 has removed the Java EE and CORBA Modules. Also the Oracle JDK’s javax.imageio JPEG Plugin now does not supports Images with alpha.