×

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.

  1. Switch expressions
  2. Text Blocks
  3. Dynamic CDS Archives
  4. ZGC – Uncommit unused memory
  5. Reimplement the legacy Socket API
  6. New Methods for Creating DOM and SAX Factories
  7. 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.


Related Topics

Compare Two Times in Java

Definition The compareTo() function of the LocalTime class is used to compare times. The class's compareTo() method compares two LocalTime objects. Here we have two objects that have to be compared: the...

4 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Collection Interfaces in Java with Examples

In this tutorial, we will discuss collection interfaces in Java with Examples. Introduction The Collection Interface is an individual from the Java Collections Framework. It is a root point of interaction of...

4 minutes read.

Example of Static import in Java

Static keyword Java's static keyword is mainly used to control memory. Variables, methods, blocks, and nested classes are compatible with the static keyword. Instead of an instance, the static keyword is...

3 minutes read.

JDBC vs ODBC

Difference Between JDBC and ODBC ODBC: ODBC (Open Database Connectivity) is the accepted method for accessing databases among organisations and programmers. A database is linked to other programmes, such as word processors, spreadsheets,...

4 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

Java Tokens

Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions...

4 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Check whether a Number is a Power of 4 or Not in Java

There are many ways to figure out if an integer is a power of 4. This section will go over a variety of techniques for figuring out whether or not...

11 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Java SHA256

Definition: In cryptography, SHA is a hash function that takes 20 bytes of input and produces an approximate 40-digit hexadecimal integer as the hash result. Class for Message Digest: Java's MessageDigest Class,...

2 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.