The new Java is here!
In the last few years, that’s been really frequent news. I’m still not used to the new release schedule of getting new Java two times a year rather than every five.
What I see as the best thing in this rapid release cycle is that it’s waking up the sleepy Java community and making it much more active compared to previous years. Of course, the volume is not even close to the online community King JavaScript, but with a more frequent release cycle, things seem to be heading in the right direction. That’s definitely great news for Java or full-stack developers.
I like the idea of Agile development in Java and getting small releases with a few new features and reworks every six months. It makes the language interesting and exciting again and proves the point that you don’t need revolutionary changes (like streams and lambdas in Java 8) just to justify a release. A large number of small improvements is just as good.
Let’s take a look at the new Java 13 features and improvements, like the new ways of using switch expressions and the text blocks feature that was originally intended for Java 12.
Switch expressions were introduced in Java 12 as a preview feature. Over the last few months, the community sent feedback to Oracle and now in Java 13, they’ve released some improvements programmers were asking for.
Let’s quickly review the switch expressions that were introduced in Java 12 and then talk about improvements that Oracle has made for this release.
Switch was always an error-prone component and generally rarely used in everyday development. Having to type break; at the end of every case always seemed like boilerplate. Java 12 addressed this by doing two things: it made switch an expression (not just a statement), which means that it now returns a value that can be assigned to a variable, or serve as a return statement, or any other use case you might think of. The second thing Java 12 did was get rid of annoying brake; by making switch not fall-through between cases when you use the new syntax case -> instead of case:.
Of course, every new Java version is backward-compatible, using old syntax gives you old fall-through behavior. None of the legacy code is broken or has changed behavior.
// Legacy Switch Statement
private String javaReleaseDate(int version) {
String result = "TBD";
switch (version){
case 12:
result = "19.3";
break;
case 13:
result = "19.9";
break;
};
return result;
}
// Arrow Syntax Switch Expression // no fall-through
private String javaReleaseDate(int version) {
return switch (version) {
case 12 -> "19.3";
case 13 -> "19.9";
default -> "TBD";
};
}
Due to community feedback on switch expressions, in Java 13 if you want to use switch as an expression and not a statement, and you want to use colon syntax, the keyword brake is replaced with the keyword yield. This change removes confusion and makes it obvious if you are using switch as an expression or as a statement, making it way less likely to unintentionally use something you didn’t want. The yield statement is very similar to the return statement; when case is matched it returns the current branch result without the fall-through.
// Switch Expression using yield over brake
private String javaReleaseDate(int version) {
return switch (version){
case 12: yield "19.3";
case 13: yield "19.3";
default: yield "TBD";
};
}
This feature is still in preview, so, in coming Java releases we might see further changes and improvements.
Text blocks are just a small piece of the huge upcoming feature “Raw String Literals” that was planned for Java 12, but due to community feedback raising way too many important questions, the feature was postponed.
Text blocks allow a better way of writing, and more importantly reading, multi-line text inside Java code. This was longed for in Java, especially because other languages that run on Java virtual machines, like Kotlin and Scala, have had support for multi-line text for quite some time now.
The main problem text blocks solve is writing code from other languages inside your Java code. Previously, you would always need boilerplate like /n for line breaks at the end of every line, which makes code error-prone and hard to read.
Text blocks give you a better way to write multi-line text in Java. They use triple quotation marks as delimiters and can be used anywhere regular string can.
// Multi line text with regular String
String html = "<html>\n" +
" <title>\n" +
" Text Blocks\n" +
" </title>\n" +
" <body>\n" +
" <p>...</p>\n" +
" </body>\n" +
"</html>\n";
// With Text Blocks
String html = """
<html>
<title>
<p>Text Blocks</p>
</title>
<body>
<p>...</p>
</body>
</html>""";
The Java compiler will compile this to regular string and there will be no hint if it was originally string or text block.
Note that text blocks cannot be used on a single line: opening quotes must be followed by a line terminator or code will not compile.
There are many more changes in Java 13. If you take a look at the official changelog, you will see a rather large list of new features but also deprecated and to-be-deprecated components.
For developers, the two that we just covered are the most interesting and something you can use immediately. Just keep in mind that both features are in preview status and might evolve in feature releases.
Originally published by Oskar at medium.com
.
#java