I know it has been 3 months since Java14 got released & you guys already have checked it. But still I thought to have this page where I can share a few code snippets to use Java14 features.
So first writing, what are those features which as a common developer I must be interested in -
a) Pattern Matching for instanceof (Preview feature) - This we will see in the example below.
b) Packaging Tool (Incubator) - In Java9, we had jlink to create jar with specific jmodules rather packaging whole rt.jar.
This time we are getting jpackage tool for packaging self-contained Java applications. This I have not tried yet.
c) NUMA-Aware Memory Allocation for G1 - For this first need to understand Numa, as it was earlier used for Parallel GC.
d) Non-Volatile Mapped Byte Buffers - I have not tried this but it will be used while accessing or writing to the files in faster
way. So you can try this.
e) Helpful NullPointerExceptions - It just provides more information about the reason of exception in your code, so in
exception message itself you will get the code which is causing the exception. For this you need to use -
XX:+ShowCodeDetailsInExceptionMessages as jvm arguments while starting your code. Below given code you can
execute to see its results.
f) Records (Preview feature) - This we will see in the example below. And note this are shallow immutable. There is no
method to change the property value, there are only getter methods. All the values are assigned via constructor only.
g) Switch Expressions (Standard) - This we will see in the example below.
h) Removed the Concurrent Mark Sweep (CMS) Garbage Collector - Now this GC is no more in Java. So make
necessary configuration changes of your application/s
i) Z Garbage Collector (ZGC) - It is introduced as Experimental feature, similarly somewhere I heard that 'Shenandoah' is
another GC introduced as Experimental, I think it is for OpenJDK.
j) Text Blocks (Second Preview) - It is marked as second preview because it was introduced earlier in Java13 & still it is not
confirmed, so marked as second review. This is shown in below example.
k) Foreign-Memory Access API (Incubator) - An incubator module, allow Java API to access foreign memory outside of
the Java heap. Its a interesting feature & check it once to see how 'VarHandle', 'MemorySegment' & 'MemoryAddres'
classes are used to interact with the memory.
So first writing, what are those features which as a common developer I must be interested in -
a) Pattern Matching for instanceof (Preview feature) - This we will see in the example below.
b) Packaging Tool (Incubator) - In Java9, we had jlink to create jar with specific jmodules rather packaging whole rt.jar.
This time we are getting jpackage tool for packaging self-contained Java applications. This I have not tried yet.
c) NUMA-Aware Memory Allocation for G1 - For this first need to understand Numa, as it was earlier used for Parallel GC.
d) Non-Volatile Mapped Byte Buffers - I have not tried this but it will be used while accessing or writing to the files in faster
way. So you can try this.
e) Helpful NullPointerExceptions - It just provides more information about the reason of exception in your code, so in
exception message itself you will get the code which is causing the exception. For this you need to use -
XX:+ShowCodeDetailsInExceptionMessages as jvm arguments while starting your code. Below given code you can
execute to see its results.
f) Records (Preview feature) - This we will see in the example below. And note this are shallow immutable. There is no
method to change the property value, there are only getter methods. All the values are assigned via constructor only.
g) Switch Expressions (Standard) - This we will see in the example below.
h) Removed the Concurrent Mark Sweep (CMS) Garbage Collector - Now this GC is no more in Java. So make
necessary configuration changes of your application/s
i) Z Garbage Collector (ZGC) - It is introduced as Experimental feature, similarly somewhere I heard that 'Shenandoah' is
another GC introduced as Experimental, I think it is for OpenJDK.
j) Text Blocks (Second Preview) - It is marked as second preview because it was introduced earlier in Java13 & still it is not
confirmed, so marked as second review. This is shown in below example.
k) Foreign-Memory Access API (Incubator) - An incubator module, allow Java API to access foreign memory outside of
the Java heap. Its a interesting feature & check it once to see how 'VarHandle', 'MemorySegment' & 'MemoryAddres'
classes are used to interact with the memory.
Below is the Person interface we are creating & it is implemented by PersonImpl class further to have the POJO. Manytimes we follow this way to create the POJOs. But here point is that we have to create a separate class which does nothing except holding & getting some assigned values. And it shows lot of code in the application where many such POJOs are being used.
Person.java
PersonImpl.java
Now above we have not overridden hashCode(), equals() & toString() methods yet. And we also want to avoid above lines of POJO where we are not doing any processing. For this we can use record which will provide us all the POJO features including the default implementations of above 3 methods also like shown below -
PersonRecords.java
Thats it, our POJO is ready to use. Below is the code to check the above record & some new features of Java14. Later is the output also attached.
Example.java
After this keep an eye on Project Loom, for virtual threads or Fibres which we may see in jdk15 & it will be revolutionary feature in the world of multithreading making the multithreading applications more efficient.
Through out this journey of Java after Java9, there have been ups & downs in its modules also.
Java9 had 98 modules. Then Java11 to Java13 had 71 modules. Now Java14 has 73 modules.
I have not seen these changes properly but I see java.se.ee module was in Java9 but it was deprecated one & it was removed soon also.
Java9 had 98 modules. Then Java11 to Java13 had 71 modules. Now Java14 has 73 modules.
I have not seen these changes properly but I see java.se.ee module was in Java9 but it was deprecated one & it was removed soon also.