Java 13 New Features
The Java SE Platform's JDK 13 version is an open-source reference implementation defined by the Java Community's JSR 388 Process. The necessary modifications made in Java 13 are listed below. It's not a comprehensive list. Users can also obtain other minor adjustments from the JEPs.
- Switch expressions
- Text Blocks
- Dynamic CDS Archives
- ZGC – Uncommit unused memory
- Reimplement the legacy Socket API
- New Methods for Creating DOM and SAX Factories
- Miscellaneous
1. Switch expressions: Switch expression functionality was present in JDK12. The update entails extending the switch statement to allow for both statement and expression usage, with both forms supporting either conventional simplifying the control flow and scoping behaviour. By including yield, Java 13 improved upon the prior version. In Java 12, the switch statement can be used as follows:
private static String testSwitch(String value) {
return switch (value) {
case "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY" -> "Weekday";
case "SATURDAY", "SUNDAY" -> "Weekend";
default -> "Invalid";
};
}
With Java 13, we can use yield to convert the phrase above into the more conventional switch statement:
private static String testSwitch(String value) {
return switch (value) {
case "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY": yield "Weekday";
case "SATURDAY", "SUNDAY": yield "Weekend";
default: yield "Invalid";
};
}
A switch expression can employ a conventional switch block with "case L:" switch labels, just like a switch statement can (suggesting logical fallacy).
2. Text Blocks: A text block is a multi-line string literal that does not require the majority of the line to automatically format itself using escape sequences consistently and provides the developer with formatting control as needed. This feature aims to make the work of building Java applications simpler by making it simple to communicate spanking strings multiple lines of source code while eliminating escape sequences in frequent situations.
to describe before Java 13, If a string is displayed across multiple lines, an escape character must be used to display it, as seen below: spanking threads of
String sql = "SELECT * \n" +
" FROM Table\n" +
" WHERE id = 1\n";
Java 13 allows us to write the code above as:
String html = """
SELECT *
FROM Table
WHERE id = 1
""";
One new literal type using the Java language is the text block. However, it gives more expressiveness and less unintentional complication than a string literal and can be used to signify a line everywhere one might occur. An opening delimiter and a closing delimiter surround a text block containing 0–more content characters three characters in double quotes ("") are used as the opening delimiter, followed by one or more white spaces, a line terminator, and then another three double-quote characters. The opening delimiter's line terminator marks the start of the content, which starts at character one.
3. Dynamic CDS Archives: Compared to the CDS default archive, AppCDS to archive application classes in HotSpot offers extra startup and memory benefits. But currently, using A Java application's AppCDS requires a three-step process.
- Follow the archive
- Utilising the newly constructed class list, dump an archive.
- Make a class list by performing a trial or trial runs.
Additionally, only apps that employ built-in class loaders can use this function. By removing trial runs (step 1 above), dynamic archiving made possible by a command-line option will make it easier to utilise AppCDS. It will also handle both built-in and custom class loaders evenly and effectively.
Automatic archive generation might be performed as a follow-up improvement to this JEP during an application's initial run. This would do away with explicitly creating the archive (step 2 above). Then, CDS/AppCDS usage might be fully transparent and automatic.
The -XX:ArchiveClassesAtExit option creates a shared archive dynamically when an application ends.
The dynamically generated archive is built on top of the standard system archive included with the JDK image that is now running. For each application, a top-layer archive file that is distinct is created. The -XX:ArchiveClassesAtExit option allows the user to input the dynamic archive's filename as a parameter. For instance, the programme below generates jcg.jsa:
Java Code Geeks: %bin/java -XX:ArchiveClassesAtExit=jcg.jsa -cp jcg.jar
Using this dynamic archive, run the identical application:
Java Code Geeks: %bin/java -XX:SharedArchiveFile=jcg.jsa -cp jcg.jar
4. ZGC – Uncommit unused memory: Even when memory has been idle for a considerable time, ZGC does not yet uncommit and restore it to the operating system. Not all programmes and environments benefit from this behaviour, especially when memory footprint has involved an issue.
The uncommit feature will be turned on by default. But regardless of the policy, ZGC should never release memory for the heap shrinks below its minimal size (-Xms). This indicates that the uncommit feature is, in fact, disabled if the JVM is launched with a minimum heap size (-Xms) comparable to the largest heap size (-Xmx). To specifically turn off this feature, use the option -XX:-ZUncommit.
Using -XX:ZUncommitDelay=, an uncommit delay can be set(300 seconds is the standard). This delay establishes the minimum time that memory must be idle before it can be released.
5. Reimplement the legacy Socket API: The Java Network. Input socket and Java.net. JDK 1.0 introduced the ServerSocket APIs and the implementations that support them. It isn't easy to upkeep and troubleshoots the performance because it combines old Java and C code. The implementation used the thread stack as the I/O buffer, a strategy that necessitated repeatedly increasing the thread stack's default size.
To facilitate asynchronous close, the application takes advantage of native data structure, which has, over time, been a source of subtle reliability and porting concerns. Additionally, the implementation has several concurrency problems that need to be fixed. The existing approach is unsuitable for its intended use in a future world where fibres park threads rather than blocking them in native procedures.
This will be swapped out by a more straightforward, up-to-date solution in Java 13 that is also simple to maintain and troubleshoot. User-mode threads, often known as fibres, can be easily adapted to work with the new implementation.
6. New Methods for Creating DOM and SAX Factories: New methods have been added to create factories with DOM and SAX with built-in Namespace support. These methods are prefixed with "NS," which stands for NamespaceAware, over their current counterparts. The new techniques are listed below:
- newNSInstance()
- newDefaultNSInstance()
- newNSInstance(String factoryClassName, ClassLoader classLoader)
With these new techniques, a parser produced by the factory will, by default, be NamespaceAware. As in the example sentence that follows:
DocumentBuilder db = DocumentBuilderFactory.newDefaultNSInstance().newDocumentBuilder();
It is equivalent to:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
7. Miscellaneous: To java.nio.file, three additional methods have been introduced. The use of file system providers that consider using a file's contents as a file system is made simpler by fileSystems.
- newFileSystem(Path)
- newFilesystem(Path, Map)
- newFilesystem(Path, Map, ClassLoader)
java.nio.ByteBuffer and the other buffer types in java.nio now defines absolute bulk get and put methods to move contiguous byte sequences without regard to or effect on the buffer position.
- There is now a flexible command-line option called -XX:SoftMaxHeapSize=. It only works if the Z garbage collector is turned on (-XX:+UseZGC). When set, the GC will make an effort not to increase the heap's size past the value supplied unless the GC determines that doing so is required to prevent an OutOfMemoryError. It is not permitted to set the softmax heap size to a value higher than the maximum heap size (-Xmx). It defaults to a discount equal to the full heap size if not set on the command line.
- PKCS#11 v2.40 functionality has been added to the SunPKCS11 provider. More algorithms are now supported by this update, including the SHA-2 message-stamping family digests-based DSA signatures, the AES/GCM/NoPadding cypher, and SASSA-PSS signatures when the underlying PKCS11 library provides the necessary PKCS11 methods.
What is the best Java version?
In 2022, Java SE 11 OR 17 will still be the favored production standard. Although versions 9 and 10 have been made available, neither will provide LTS. Since java has maintained its reputation since its initial release in 1996. as one of the most dependable, safe, and cross-platform programming languages.
What is Java's most recent version, 2022?
Java 18 is the newest version of java as of March 2022. Following in September 2022 will be Java 19. Java 17, published in September 2021, is the software's last LTS (long-term support) version.
What distinguishes JDK from JRE?
One uses the JDK (Java Development Kit) to create Java programmes. Compilers, debuggers, and other development tools are all included in JDK. JRE (Java Runtime Environment) is the JVM's implementation and was created specifically to run Java programmes.