Everything you need to know about the Java 13

 

Java has just been released on September 17th, 2019

 

Let’s take the time to look at what’s inside and see what makes it different from the older versions and know all about the Java 13.

Java 13, in essence, doesn’t have too many major updates (which are usually called a JEP (Java Enhancement Proposal).

Here’s a list of JEPs that were delivered in the new Java 13:

  • Switch Expressions (Preview)
  • Text Blocks (Preview)
  • Reimplement the Legacy Socket API
  • Dynamic CDS Archives
  • ZGC: Uncommit Unused Memory




It’s most likely that the two updates are going to be the most interesting for the Java developers. The really interesting thing that catches your attention is that the said updates introduce new syntax in the Java language.

Switch Expressions (Preview)

To state the obvious, switch expressions have already been introduced in Java 12. To make a long story short, JEP 325 added a much-simplified form of a switch which looks like the following below:

T result = switch (arg) {
case L1 -> e1;
case L2 -> e2;
default -> e3;
};

If you’re not aware of the new syntax, here’s an example of how you can use it:

int result = switch (s) {
case “Foo” -> 1;
case “Bar” -> 2;
default       -> throw new IllegalArgumentException(“Oops!”);
};

Java 13 adds a brand new yield keyword that enables returning a value from a switch block, like below:

int result = switch (s) {
case “Foo” -> 1;
case “Bar” -> 2;
default -> {
System.out.println(“Neither Foo nor Bar, hmmm…”);
yield 0;
}
};




Above, we can clearly see that a switch expression can be used to calculate a value for a local variable – and it does that without assigning the value in each and every case block. The yield statement is used in the default block so it can return a value. But, it can also be used in a regular case block.

Now, let’s head to the bad news.

You’ve most likely noticed the “preview” word in the JEP’s title. It simply means that the new syntax is not enabled by default in Java 13. 

If you want to get started with the new switch expressions, you’re going to have to run the Java compiler with –enable-preview –release 13 options, and then start your program with –enable-preview option:

$ javac -d classes –enable-preview –release 13 Test.java
$ java -classpath classes –enable-preview Test

Text Blocks (Preview)

Java 13 has another major language update – it’s text blocks that help you define multi-line strings.

For instance, an HTML snipper or an SQL query is very often defined as a multi-line string in Java. Here’s what they are commonly used like:

String html = “<html>\n” +
” <body>\n” +
” <p>Hello, world</p>\n” +
” </body>\n” +
“</html>\n”;

You’ve probably noticed a lot of \n+ and " characters that make the code a little bit longer than it should. And a lot harder to read!

Some programming languages, like Python, allows you to define multi-line strings simply by using triple quotes “”” to start and end them. Those kinds of language features help you a lot with making your code much nicer and more readable.




Finally, Java 13 introduces a feature like that (Finally!). Here’s how the example above can be re-factored and re-written in order to make it better:

String html = “””
<html>
<body>
<p>Hello, world</p>
</body>
</html>
“””;

At runtime, the string above becomes a usual String (keep in mind that the indentation spaces are removed at runtime):

<html>
<body>
<p>Hello, world</p>
</body>
</html>

Even though the new text blocks might look simple, there are a lot of query important topics around like:

  • Line terminators
  • Indentation
  • Incidental white space
  • Escape sequences
  • Concatenation

Unfortunately enough, the new text blocks are currently available only in preview mode.

The rest

JEP 353 replaces the underlying implementation of the java.net.Socket and the java.net.ServerSocker APIs.

The crucial motivation for this update was that the old implementation had a lot of problems that don’t fit well with the plans of the further Java development.




The Java sockets are the central component for networking. If the update shows a regression, it’s going to affect a lot of applications. Fortunately, the old implementation was not completely removed. If something might go wrong, the jdk.net.usePlainSocketImpl system property could be used to switch back to the old implementations. Nevertheless, the old implementation and the system property are going to be removed completely in one of the next JDK releases.

Additionally, the JEP 351 introduces a brand new option – -XX:ZUncommitDelay for the Z Garbage collector (ZGC). The option simply tells ZGC when it has to uncommit and return memory from the OS (Operating system).

And lastly, JEP 350 is an update mainly for application class-data sharing (AppCDS). The JEP enables the AppCDS to allow the dynamic archiving of classes at the end of the Java application execution.




Conclusion

In conclusion, Java 13 doesn’t have a lot of JEPs – apart from major updates. But, the good news is that the new Java release has some 2500 bug fixes and minor enhancements. Since the new text blocks and switch expressions are still at preview language features, we will most likely not see so much of a difference after migrating to the new Java version.

That’s everything you need to know about the Java 13. If you feel like something else should be included, please contact us.

Shares