JDK 13 – new functionalities and enchantments
News and improvements introduced in the latest version of the Java Development Kit.
News and improvements introduced in the latest version of the Java Development Kit.
While Java 8 is still used by more than 79% of developers of this language, on September 17 last year, version 13 of the Java Development Kit has got officially released.
In this article, I wanted to introduce news and enchantments in language semantics and JVM that Oracle added to JDK 13.
Using the vocabulary of the Java universe, each modification added to the released version is called Java Enhancement Proposals (JEP), and it has its unique number. In the “Documentation” section, the curious will find links to official sources that describe the changes in detail.
The implementation of text blocks into Java syntax is a complete novelty and one of the most anticipated.
Every Java programmer knows how tedious is assigning a hardcoded text to a String variable, e.g., in JSON or XML file format.
During such operations, one needs to remember about escaping special characters with a ‘slash’ or adding newlines and tabs to appropriate places.
Modern programming environments, e.g., IntelliJ IDEA, automate this process. However, the readability of such variables leaves a lot to be desired, as the following example illustrates:
String pastedJson = "{n" +
"t"id": "1",n" +
"t"type": "SUV",n" +
"t"name": "TOYOTA RAV 4",n" +
"t"image":n" +
"tt{n" +
"ttt"url": "dir/0001.jpg",n" +
"ttt"width": 200,n" +
"ttt"height": 200n" +
"tt},n" +
"t"thumbnail":n" +
"tt{n" +
"ttt"url": "dir/thumbnails/0001.jpg",n" +
"ttt"width": 160,n" +
"ttt"height": 160n" +
"tt}n" +
"}";
Thanks to text blocks, complex, multi-line text can be pasted. Starting and ending with three double quotation marks make it correctly readable for Java.
The above example we can write using a text block in the following way:
String jsonInTextBlock =
"""
{
"id": "1",
"type": "SUV",
"name": "TOYOTA RAV 4",
"image": { "url": "dir/0001.jpg",
"width": 200, "height": 200 },
"thumbnail": { "url": "dir/thumbnails/0001.jpg",
"width": 160,
"height": 160 } }
""";
This feature will unquestionably improve the quality of work on such complex text of each developer.
Text blocks are enabled only in preview mode.
To understand this small modification of the switch in Java 13, we should note the groundbreaking changes of this instruction introduced in the previous version.
Starting from JDK 12, the switch command can be treated not only as a statement but also as an expression that can return a value and whose result can get assigned to a variable.
Besides, the new syntax allows us to write a functional-like cases suggestive of lambda and combine several cases in one line.
Below is an example of a switch written using the mentioned elements:
// award is an enum variable
int y = switch (award) {
case GOLD, AMBER -> 1;
case SILVER -> 2;
case BRONZE -> 3;
default -> {
System.out.println("Not on podium");
break -1;
}
};
In Java 13, to return the value from a case or default, the newly introduced keyword yield should be used instead of break. So the above example would be changed to:
// award is an enum variable
int y = switch (award) {
case GOLD, AMBER -> 1;
case SILVER -> 2;
case BRONZE -> 3;
default -> {
System.out.println("Not on podium");
yield -1;
}
};
Detailed information from the documentation regarding the use of these two keywords in the switch says:
The two statements, break (with or without a label) and yield,
facilitate easy disambiguation between switch statements and
switch expressions: a switch statement but not a switch expression
can be the target of a break statement; and a switch expression but
not a switch statement can be the target of a yield statement.
Java language keyword yield is enabled only in preview mode.
A new garbage collector – ZGC (Garbage Collector Z) debuted in Java 11. The new garbage collector aims to provide scalability, low latency, and sufficient work even in applications requiring a large heap of the order of several terabytes.
ZGC is continuously being developed and successively improved in keeping with introduced subsequent versions of Java. In JDK 13, JEP 351 enchantment causes the return of unused heap memory to the operating system.
The seemingly small change is particularly significant for users who pay for the memory used, e.g., in cloud computing services.
As part of JEP 350, the AppCDS (application class-data sharing) mechanism has expanded. As a result, we get the archive generated at the end of the application, with all classes loaded from libraries and applications that were not placed in the default archive.
It speeds up the start of applications written in Java and eliminates the need for the trial runs of AppCDS, required in earlier versions.
Almost every programmer trembles to think about working with legacy code3.
Obsolete and widely known net. Socket and java.net.ServerSocket, whose implementations still remember JDK 1, got a new implementation – NioSocketImpl. It was written in mind of easier maintenance and debugging of Java applications.
One of the essential points from the documentation about the new implementation is that:
Socket operations using timeouts (connect, accept, read) are
implemented by changing the socket to non-blocking mode and polling
the socket.
The implementation is also adaptable to the Loom Project4, which introduces and develops an entirely new approach to threads, the so-called user-mode threads, called fibers.
1. Source: sdtimes.com ↩︎
2. hardcoding – entering input or configurational data directly in source code ↩︎
3. legacy code – mostly old and not supported source code, written using technologies that don’t fit the main technology stack of the project, maintained only to provide backward compatibility ↩︎
4. Project Loom ↩︎
Are you looking for a tech partner? Searching for a new job? Or do you simply have any feedback that you'd like to share with our team? Whatever brings you to us, we'll do our best to help you. Don't hesitate and drop us a message!
Drop a message