Java 13 was released for production use on 17th September 2019. There are not a lot of developer-specific features in Java 13 because of the 6-month release cycle.
Some of the important Java 13 features are:
Switch expressions and text blocks are preview features. So you will have to enable the preview-feature settings in your project. If you are running a java program from the command line, you can enable it using the --enable-preview
switch. You can use this switch to start JShell with preview features enabled.
$ jshell --enable-preview
$ java --enable-preview --source 13 Test.java
If you are using Eclipse IDE, you can enable the preview features from the project Java Compiler settings.
This is a preview feature. It allows us to create multiline strings easily. The multiline string has to be written inside a pair of triple-double quotes. The string object created using text blocks has no additional properties. It’s an easier way to create multiline strings. We can’t use text blocks to create a single-line string. The opening triple-double quotes must be followed by a line terminator.
package com.journaldev.java13.examples;
public class TextBlockString {
/**
* JEP 355: Preview Feature
*/
@SuppressWarnings("preview")
public static void main(String[] args) {
String textBlock = """
Hi
Hello
Yes""";
String str = "Hi\nHello\nYes";
System.out.println("Text Block String:\n" + textBlock);
System.out.println("Normal String Literal:\n" + str);
System.out.println("Text Block and String Literal equals() Comparison: " + (textBlock.equals(str)));
System.out.println("Text Block and String Literal == Comparison: " + (textBlock == str));
}
}
Output:
Text Block String:
Hi
Hello
Yes
Normal String Literal:
Hi
Hello
Yes
Text Block and String Literal equals() Comparison: true
Text Block and String Literal == Comparison: true
It’s useful in easily creating HTML and JSON strings in our Java program.
String textBlockHTML = """
<html>
<head>
<link href='/css/style.css' rel='stylesheet' />
</head>
<body>
<h1>Hello World</h1>
</body>
</html>""";
String textBlockJSON = """
{
"name":"Pankaj",
"website":"JournalDev"
}""";
There are three new methods in the String class, associated with the text blocks feature.
package com.journaldev.java13.examples;
public class StringNewMethods {
/***
* New methods are to be used with Text Block Strings
* @param args
*/
@SuppressWarnings("preview")
public static void main(String[] args) {
String output = """
Name: %s
Phone: %d
Salary: $%.2f
""".formatted("Pankaj", 123456789, 2000.5555);
System.out.println(output);
String htmlTextBlock = "<html> \n"+
"\t<body>\t\t \n"+
"\t\t<p>Hello</p> \t \n"+
"\t</body> \n"+
"</html>";
System.out.println(htmlTextBlock.replace(" ", "*"));
System.out.println(htmlTextBlock.stripIndent().replace(" ", "*"));
String str1 = "Hi\t\nHello' \" /u0022 Pankaj\r";
System.out.println(str1);
System.out.println(str1.translateEscapes());
}
}
Output:
Name: Pankaj
Phone: 123456789
Salary: $2000.56
<html>***
<body> *
<p>Hello</p>** *
</body>*
</html>
<html>
<body>
<p>Hello</p>
</body>
</html>
Hi
Hello' " /u0022 Pankaj
Hi
Hello' " /u0022 Pankaj
Switch expressions were added as a preview feature in Java 12 release. It’s almost same in Java 13 except that the “break” has been replaced with “yield” to return a value from the case statement.
package com.journaldev.java13.examples;
/**
* JEP 354: Switch Expressions
* https://openjdk.java.net/jeps/354
* @author pankaj
*
*/
public class SwitchEnhancements {
@SuppressWarnings("preview")
public static void main(String[] args) {
int choice = 2;
switch (choice) {
case 1:
System.out.println(choice);
break;
case 2:
System.out.println(choice);
break;
case 3:
System.out.println(choice);
break;
default:
System.out.println("integer is greater than 3");
}
// from java 13 onwards - multi-label case statements
switch (choice) {
case 1, 2, 3:
System.out.println(choice);
break;
default:
System.out.println("integer is greater than 3");
}
// switch expressions, use yield to return, in Java 12 it was break
int x = switch (choice) {
case 1, 2, 3:
yield choice;
default:
yield -1;
};
System.out.println("x = " + x);
}
enum Day {
SUN, MON, TUE
};
@SuppressWarnings("preview")
public String getDay(Day d) {
String day = switch (d) {
case SUN -> "Sunday";
case MON -> "Monday";
case TUE -> "Tuesday";
};
return day;
}
}
The underlying implementation of the java.net.Socket and java.net.ServerSocket APIs have been rewritten. The new implementation, NioSocketImpl, is a drop-in replacement for PlainSocketImpl. It uses java.util.concurrent locks rather than synchronized methods. If you want to use the legacy implementation, use the java option -Djdk.net.usePlainSocketImpl.
This JEP extends the class-data sharing feature, which was introduced in Java 10. Now, the creation of CDS archive and using it is much easier.
$ java -XX:ArchiveClassesAtExit=my_app_cds.jsa -cp my_app.jar
$ java -XX:SharedArchiveFile=my_app_cds.jsa -cp my_app.jar
This JEP has enhanced ZGC to return unused heap memory to the operating system. The Z Garbage Collector was introduced in Java 11. It adds a short pause time before the heap memory cleanup. But, the unused memory was not being returned to the operating system. This was a concern for devices with small memory footprint such as IoT and microchips. Now, it has been enhanced to return the unused memory to the operating system.
Three new methods have been added to the FileSystems class to make it easier to use file system providers, which treats the contents of a file as a file system.
There are new methods to instantiate DOM and SAX factories with Namespace support.
//java 13 onwards
DocumentBuilder db = DocumentBuilderFactory.newDefaultNSInstance().newDocumentBuilder();
// before java 13
DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
It looks like that the 6-months release of Java has been working well. There are not many developer-specific features, but overall it’s a great release. It’s good to see the much-awaited text blocks string support.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Great updates. Thank you very much.
- Tanushree Maji