Hier gibt es diesen Artikel auf Deutsch
As the relase cycle of Java has changed we can now be excited for a new Java Release every half a year. So in September 2019 Java 13 will already come out. But don’t worry: Java developers do not need to be stressed: Today many years still use Java 8 *cough cough* uhm I mean Java 11 and the next LTS = Long Term Support-Version will be Java 17 in 2021.
So we will get two new versions of Java each year but at the same time they are mostly small releases with only a few additions. And some of the innovations are marked as preview, so you can still expect changes there after the community has been able to give feedback.
Enough talk, what are the new changes?
The following so called JPE’s = Java Enhancement Proposals are going to be relased with 13:
- Switch Expressions (preview)
- Text Blocks (preview)
- Z Garbage Collector: Uncommit Unused Memory
- Dynamic CDS Archives
- Reimplementation of the Legacy Socket API
The first two points are especially interesting for developers. That’s why I wrote a whole chapter about those.
The ZGC = Z Gargabe Collector is a new… uhm, well a new Garbage Collector, as the name implies 🙂 It promises to clean big chunks of data on the heap very quickyl. Not only that but it will also free this space if it’s not being used anymore, something that apparently has not been done yet by the old garbage collector and some applications have used a lot of memory unnecessarily after a long time running.
Dynamics CDS Archives are an extension to the with Java 5 introduced CDS = Class Data Sharing. In CDS some specific information about application is stored in CDS archives to reduce the start time of applications. Until Java 10 only the Bootstrap Class Loader was able to use those archives. With Java 10 the AppCDS = Application Class Data Sharing has been published and now also other class loaders have access to those archives. To create those archives the application needed to be run in trial runs to figure out what classes could actually be loaded in an effective way. Now with Dynamics CDS these trial runs are not needed anymore.
And there is also a new implementation of the Socket API that still comes from JDK 1. NioSocketImplementation replaces the old PlainSocketImplementation. As the name suggests it’s derived from the New I/O-Implementation.
You can find more information for example here: https://docs.oracle.com/javase/8/docs/technotes/guides/io/index.html
How do I activate preview changes?
Preview means that there might still be changes in the future for those enhancements. To enable these preview features in Java 13 you need to add a flag to the compilation of your program:
javac --release 13 --enable-preview MyClass.java
You also need to add this flag when you start your application:
java --enable-preview MyClass
If you are using Maven you need to add the flag in the pom.xml to the maven-compiler-plugin part.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>13</release>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
What are Switch Expressions?
Switch Expressions have the difference to the traditional Switch-Case statement so that you can directly return the values (and I actually mean the „return“ statement).
Note the new keyword yield instead of break.
Switch-Case example:
String name = "";
switch(personalId) {
case 1:
case 2:
name = "Fritz";
break;
case 3:
name = "Hanswurst";
break;
};
return name;
Example with Switch Expression:
return switch(personalId) {
case 1, 2: yield "Fritz";
case 3: yield "Hanswurst";
default: yield "Defaultname";
};
Since Java 12 you can also use the arrows to make the statement even simpler:
return switch(personalId) {
case 1, 2 -> "Fritz";
case 3 -> "Hanswurst";
default -> "Defaultname";
};
To be honest I myself barely use the Switch-Case statement. But from now on with the new Switch Expressions I will do everything in Switch statements, I promise!
What are Text Blocks?
Text Blocks simplify the usage of strings so that you can define a string in multiple lines without having these annoying + and „\n“ signs (for page break). For example you could do it to an SQL statement to make it more readable. This is already possible in various other programming languages like Groovy, Scala, Kotlin, C#, Swift or Python.
To create a Block simply use three quotation marks. Important: The opening quotation marks need to be in their own line!
So it was like this:
String meineSeite = "<html>n" +
"<body>n" +
"<p>Meine super Seite!</p>n" +
"</body>n" +
"</html>";
And now with text blocks – A wonderful new change! The world will never be the same! People will go out dancing in the streets! There will be festivals all dedicated to the Java god! So cherish this holy gift that we have received in the form of Text Blocks!
String meineSeite = """
<html>
<body>
<p>Meine super Seite!</p>
</body>
</html>""";
Cool, right? 🙂