What are the new Features of Java 13?
|Are you also wondering what all is new in Java 13, then you are at the right place as we are gonna discuss some of the new features introduced in Java 13.
So let’s begin with some understanding of Java versions.
As we know Java 12 was released officially in march 2019 by Oracle and Java 13 was released in September 2019 as per Oracle plan as they were gonna release two versions of Java in 2019.
The first thing to understand is that this is a minor version as all the difference between different releases of Java is how long it will be supported.
To differentiate between the minor and major versions of Java. The more important thing to look upon is the Long Term Support (LTS) date mentioned by Oracle.
Java 8 has an LTS date until March 2022 which can be extended to March 2025 on demand.
For Java 11, Oracle announced the LTS date until September 2023 which can be extended to 2026 as mentioned on their Website.
By this, we can see that two major versions of JDK are 8 and 11.
Now let’s come to the main point for what we are here, The new features of Java 13 so that you can get a better idea of Java 13 and how you can use it in a better way.
Introduction of Text Blocks –
This is the major feature of Java 13 for Java developers but unfortunately, this is only available as an experimental feature.
A Text Block is a multi-line string literal that avoids the need for most escape sequences.
Text Blocks are simply a new type of String Literal that allows writing strings in multiple lines without using String concatenation.
To simplify their use Java architects decided that a Text Block would start with a new specific delimiter(“”” -> triple quotes) and a line break. This allows simple quotes to be used within a Text Block without having to escape them.
Let’s check one of the examples of TextBlocks:
package textblock; public class Java13TextBlockExample { public static void main(String[] args) { System.out.println(""" Hello, “You are here at CodinGeek” (Yes you can even use quotes with TextBlock syntax) to check the new features of Java 13! (-> yeah this statement will be with a tab) """ // --> Yeah it will consider this end line ); } }
Output:- Hello, “You are here at CodinGeek” (Yes you can even use quotes with TextBlock syntax) to check the new features of Java 13! (-> yeah this statement will be with a tab)
If we ignore the string in brackets[()] then before this feature we used to write this code as per the example below which is not so nice to read:
String str=”Hello, \n \“You are here at CodingGeeks\”\n to check the new features of Java 13”;
The Java compiler will compile this to regular string and there will be no hint if it was originally string or Text Block.
Note:- Here the opening quotes must be followed by a terminator line otherwise code would not compile.
Now since we have understood the concept of Text Blocks and the simplification it will allow when someone wants to print HTML or JSON code:
Let’s check another Example of using TextBlocks to print JSON code.
package textblock; public class Java13TextBlockJSONExample { public static void main(String[] args) { System.out.println(""" { date: “25/01/2019”, title: “Java 13”, version: “Major” }""" ); } }
Output:-
Hello,
“You are here at CodinGeek” (Yes you can even use quotes with TextBlock syntax)
to check the new features of Java 13! (-> yeah this statement will be with a tab)
Text Blocks are also good since they retain the indentation defined by the user at the time of declaration in the source code. Only the indentation made before the first letter is deleted because it is considered accidental.
It should also be noted that a line break defined before the specific end delimiter will be considered as voluntary and will be taken into account in the String thus built at runtime.
Finally, this introduction of TextBlocks has led to the addition of three new utility methods within the String class that will be helpful for Java Developers and we will discuss them in future in detail.
- String::stripIndent() – This method is used to remove white space that are accidentally added to the content of a TextBlock.
- String::translateEscape() – this is used to translate escape sequences.
- String::formatted(Object… args ) – this would simplify value substitution in a Text Block
Changes On Switch Expressions
Switch Expressions have been introduced as an experimental feature in Java 12. A modification has been proposed for Java 13 and the Switch Expressions still remain in preview mode in this new version of JDK.
Java 13, therefore, introduces the new yield keyword that must be used to return a value in an Expression Switch where the right side of the “case L->” syntax is not just a simple expression.
Once the Switch Expression feature of Java 13 is enabled, the following code is valid:
package switchblockandexpression; public class SwitchExpressionWithYieldExample { public static void main(String[] args) { System.out.println("Mood for JANUARY -> " + getMonthMood(Month.JANUARY)); System.out.println("Mood for FEBRUARY -> " + getMonthMood(Month.FEBRUARY)); System.out.println("Mood for MARCH -> " + getMonthMood(Month.MARCH)); System.out.println("Mood for APRIL -> " + getMonthMood(Month.APRIL)); } private static String getMonthMood(Month month) { // It is not simply a block that executes some code // rather it is an expression that can return a value return switch (month) { case JANUARY, FEBRUARY -> "Wow"; // Witness the pattern matching no need for "breaks" case MARCH -> "Favourite month"; default -> { // generally needed when some extra code is to be executed yield "Whatever.. I dont care."; } }; } private enum Month { JANUARY, FEBRUARY, MARCH, APRIL } }
Output:-
Mood for JANUARY -> Wow
Mood for FEBRUARY -> Wow
Mood for MARCH -> Favourite month
Mood for APRIL -> Whatever.. I dont care.
It is also possible to use the “case L:” syntax of conventional switch blocks within Switch Expressions by combining it with the use of the keyword yeild.
So if we replace the getMonthMood as mentioned below the output still remains the same but we no longer need to add break statements with every case.
private static String getMonthMood(Month month) { // It is not simply a block that executes some code // rather it is an expression that can return a value return switch (month) { case JANUARY: case FEBRUARY: yield "Wow"; // See with "yield" we do not need the break statement case MARCH: yield "Favourite month"; default: yield "Whatever.. I dont care."; }; }
Output:-
Mood for JANUARY -> Wow
Mood for FEBRUARY -> Wow
Mood for MARCH -> Favourite month
Mood for APRIL -> Whatever.. I dont care.
It is important to know that there is a case of a Switch Expression for every possible value of input i.e it must be exhaustive. Normally this does not apply to traditional switch declarations.
In simple language we can say, this means that we have to always define a default clause within a Switch Expression.
These changes in JDK will lay grounds for pattern matching in future Java versions like other languages that have mastered this syntax like Scala.
Rewriting Of The Socket API
It is mainly based on the java.net.Socket and java.net.ServerSocket classes. A rewriting of these classes and more generally of the Socket API was, therefore, necessary in order to provide the JDK with a more modern implementation.
Implementation of this new feature in Socket API will provide a simpler and more modern code that will make debugging and maintenance easier for Java developers.
Besides, this new implementation aims to promote its interoperability with Fibers, Fibers are User-Mode Threads, which are currently being explored within the Loom project.
For your information, Loom project plans to introduce a lighter competition model within the JDK.
On the latest prototypes available within OpenJDK, a new Fiber class has been added running in parallel with the Thread Class. The future API made available to Java developers should be similar to that of the Thread class with two differences:
- A Fiber will encapsulate any task in a User-Mode work context. This will allow you to suspend and resume a task directly within the Java runtime rather than via the kernel.
- A User-Mode task scheduler, of the ForkJoinPool API type, will be used.
For the moment, no target version of the JDK has been announced for the Loom project.
This feature can be understood in short that the original implementations have not been removed from JDK yet. It is possible to continue using them with the following option while launching the JVM.
-Djdk.net.usePlainSocketImpl
Improvements To The Garbage Collector ZGC
The Garbage Collector is an essential part of the Java virtual machine. This Garbage Collector was offered on an experimental basis and targeted only 64-bit Linux environments.
In JDK 13 there are enhancements in ZGC (Z Garbage Collector ) to return unused memory to the operating system. ZGC is a low latency collector, which currently does not return unused memory to OS.
As the JDK evolves, to meet the new needs of Java applications, new implementations have been proposed by Oracle or even by third-party contributors such as Red Hat.
JDK 13 builds are available for Linux, MacOs and Windows.
All our Java 13 code examples are available here.
Ajeet Sharma ?has made significant contributions to this article.
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?