Java 13 was released for General Public usage on 17 September 2019. But, Similar to Java 12, this wasn’t considered a major release too because it didn’t offer much developer-specific features. You can download Java 13 by clicking here.
Java 13 Features
JEP 350: | Dynamic CDS Archives |
JEP 351: | ZGC: Uncommit Unused Memory |
JEP 353: | Reimplement the Legacy Socket API |
JEP 354: | Switch Expressions (Preview) |
JEP 355: | Text Blocks (Preview) |
Check out: JAVA 11 Features with examples
1 2 3 | $ java -XX:ArchiveClassesAtExit=hello_world.jsa -cp hello_world.jar $ java -XX:SharedArchiveFile=hello_world.jsa -cp hello_world.jar |
The Objective behind CDS is to improve the startup process by creating the class data archive once and then reuse it. In this way, JVM need not create archive again and again.
2. JEP 351 – ZGC: Uncommit Unused Memory
Z Garbage Collector was introduced in Java 11 as part of JEP-333. It gives a short pause time during clean up the heap memory. But the challenge was that It didn’t return the unused heap memory to the Operating System.
Now, JEP-333 has been enhanced as part of this JEP-351 so that now ZGC returns the unused memory to the Operating System.
3. JEP 353 – Reimplement the Legacy Socket API
The underlying implementation of the java.net.Socket and java.net.ServerSocket APIs are very old and have been enhanced as part of this JEP-353 which introduces a new implementation for Socket API.
Java 13 introduced NioSocketImpl, is a drop-in replacement for PlainSocketImpl.
But, if you want to use the old implementation, you can easily switch by setting jdk.net.usePlainSocketImpl
as a system property.
4. JEP 354 – Switch Expressions Enhancements
JEP 354 is an enhancement to the Switch Expression, added as a preview feature in Java 12. It’s not a major change. Almost everything is the same as Java 12 expect a new keyword ‘yield‘ has been added which replaced the ‘break‘ keyword to return the value from the case statement.
Let me show you how it was before Java 12, how it was in Java 12, and now what change has been brought by Java 13 in Switch Statement.
Before Java 12: It was the traditional Switch Statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class SwitchStatement_13 { public static void main(String args[]) { getValue(1); } private static String getValue(int number) { String value = ""; switch (number) { case 1: value = "Elephant"; break; case 2: value = "Dog"; break; case 3: value = "Tiger"; break; case 4: value = "Cat"; break; default: value = "No Idea"; break; }; return value; } } |
In Java 12: ‘break’ can be used to return the value now.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class SwitchStatement_12 { public static void main(String args[]) { getValue(1); } private static String getValue(int number) { String value = switch (number) { case 1: break "Elephant"; case 2,3: break "Dog"; case 4: break "Tiger"; case 5,6,7,8: break "Cat"; default: break "No Idea"; }; return value; } } |
In Java 13: ‘break’ won’t work anymore as it was in Java 12. You need to use ‘yield’ to return the value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class SwitchStatement_13 { public static void main(String args[]) { getValue(1); } private static String getValue(int number) { String value = switch (number) { case 1: yield "Elephant"; case 2,3: yield "Dog"; case 4: yield "Tiger"; case 5,6,7,8: yield "Cat"; default: yield "No Idea"; }; return value; } } |
5. JEP 355 – Text Blocks
JEP 355 allows you to create a multiline string or you can say a text-block. You need to write a multiline string inside a pair of triple double-quotes.
Just to keep in mind, the beginning triple double quotes must be followed by a line terminator.
Before Java 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class RegularMultilineString_BeforeJava13 { public static void main(String args[]) { regularMultiineStringStructure(); } private static void regularMultiineStringStructure() { String regularMultiline_String = "This\n" + "is\n" + "the\n" + "Multiline String"; System.out.println("Regular Multiline String:\n"+ regularMultiline_String); } } |
Output:
1 2 3 4 5 | Regular Multiline String: This is the Multiline String |
In Java 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class TextBlock_Java13 { public static void main(String args[]) { textBlockStringStructure(); } @SuppressWarnings("preview") private static void textBlockStringStructure() { String textBlock_String = """ This is the TextBlock"""; System.out.println("Text Block String:\n" + textBlock_String); } } |
Output:
1 2 3 4 5 | Text Block String: This is the TextBlock |
Java 13 Features...!!! Share on X
Other Useful References:
- What is new in Java 13